1

我想使用 Texture2D 作为基本枚举。类似于颜色的工作方式。IE。颜色:黑色

这不会编译,因为您不能使用 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;
4

1 回答 1

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;
于 2012-07-09T12:58:20.793 回答