C# XNA按字符串设置颜色

C# XNA按字符串设置颜色,c#,function,xna,C#,Function,Xna,所以我想做一个函数,在这个函数中,你输入一种颜色(比如白色、红色、海蓝宝石等等),它将矩形的颜色设置为你输入的颜色。最好的方法是什么 我明白了: public void setVisible(GraphicsDevice gd ,SpriteBatch sb, object c) { rec = new Rectangle(ButtonXPosition,ButtonYPosition,ButtonWidthSize,ButtonHeightSize); Tex

所以我想做一个函数,在这个函数中,你输入一种颜色(比如白色、红色、海蓝宝石等等),它将矩形的颜色设置为你输入的颜色。最好的方法是什么

我明白了:

public void setVisible(GraphicsDevice gd ,SpriteBatch sb, object c) {

        rec = new Rectangle(ButtonXPosition,ButtonYPosition,ButtonWidthSize,ButtonHeightSize);
        Texture2D pixel = new Texture2D(gd, ButtonWidthSize,ButtonHeightSize);
        sb.Draw(pixel,rec,Color.c);
    }

您可以通过名称检索预设颜色,并进行一些反射:

var prop = typeof(Color).GetProperty(nameOfColor);
if (prop != null)
    return (Color)prop.GetValue(null, null);
return default(Color);

您可以通过名称检索预设颜色,并进行一些反射:

var prop = typeof(Color).GetProperty(nameOfColor);
if (prop != null)
    return (Color)prop.GetValue(null, null);
return default(Color);

给您。@RyanBennett不是枚举,它是一个具有表示预设值的静态属性的结构。好了。@RyanBennett不是一个枚举,它是一个具有表示预设值的静态属性的结构。值得注意的是,它还可以很高兴地获得属性,如
R
a
PackedValue
;)好吧,你得留点东西给读者做练习!值得注意的是,这也将很高兴地获得属性,如
R
A
PackedValue
;)好吧,你得留点东西给读者做练习!