XNA按字符串设置颜色

Dal*_*lox 1 c# xna function

所以我想制作一个颜色(如白色,红色,海蓝宝石等)的功能,它将矩形的颜色设置为你放入的颜色.最好的方法是什么?

我懂了:

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);
    }
Run Code Online (Sandbox Code Playgroud)

Col*_*ell 5

您可以通过一些反射按名称检索预设颜色:

var prop = typeof(Color).GetProperty(nameOfColor);
if (prop != null)
    return (Color)prop.GetValue(null, null);
return default(Color);
Run Code Online (Sandbox Code Playgroud)

  • 那么,你必须留下一些东西作为练习给读者! (2认同)