2012-07-09 71 views
1

我想使用Texture2D作为基础枚举。类似于Color Works的方式。即。 Color.Black有没有办法使用Texture2D作为枚举的基础

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

回答

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; 
+1

我喜欢这一个,它基本上做同样的事情作为枚举。 – 2012-07-09 13:08:09

相关问题