有没有办法使用Texture2D作为枚举的基础

时间:2012-07-09 12:36:11

标签: c# enums xna

我想将Texture2D用于基本枚举。类似于Color Works的方式。即。 Color.Black

这不能编译,因为你不能使用Texture2D作为基础,我使用这段代码来演示我想要的东西。

public class Content
{
    public Dictionary<string,Texture2D> Textures =new Dictionary<string, Texture2D>();
}


public enum Texture:Texture2D
{
    Player = Content.Textures["Player"],
    BackGround = Content.Textures["BackGround"],
    SelectedBox = Content.Textures["SelectedBox"],
    Border = Content.Textures["Border"],
    HostButton = Content.Textures["HostButton"]
}

然后可以像

一样使用
Texture2D myTexture= Content.Texture.Player;

1 个答案:

答案 0 :(得分:3)

您不能将对象用作枚举的基础。你可以做的是将不同的纹理作为静态属性添加到类中:

public static class Texture
{
    public static Texture2D Player { get; private set; }
    public static Texture2D BackGround { get; private set; }
    ...

    static Texture()
    {
        Player = Content.Textures["Player"];
        BackGround = Content.Textures["BackGround"];
        ...
    }
}

这样你可以按照自己的意愿使用它们:

Texture2D myTexture = Texture.Player;
相关问题