我正在处理一个项目,部分要求是模拟绘制一个矩形,根据模拟中出现的某些标志/事件改变颜色。这是一个带有红绿灯的驾驶模拟,所以事件可能包括红绿灯颜色变化,控制输入等。这是我现在的代码。
private static Rect rectangle = new Rect(0,0,50,50);
private static Texture2D _staticRectTexture;
private static GUIStyle _staticRectStyle;
public static void GUIDrawRect(Color color )
{
if( _staticRectTexture == null )
{
_staticRectTexture = new Texture2D( 1, 1 );
}
if( _staticRectStyle == null )
{
_staticRectStyle = new GUIStyle();
}
_staticRectTexture.SetPixel( 0, 0, color );
_staticRectTexture.Apply();
_staticRectStyle.normal.background = _staticRectTexture;
GUI.Box( rectangle, GUIContent.none, _staticRectStyle );
}
public void OnGUI() {
GUIDrawRect (Color.blue);
}
现在,基本上问题是这个方法只能在OnGUI()
方法中调用。需要能够从任何类/方法调用此方法。例如,当调用我的ChangeLight()
方法(更改停止灯)时,我希望能够在其中调用GUIDrawRect(Color color)
来更改矩形。
矩形必须在GUI上,因为我只是将它绘制到主计算机(而不是渲染场景的多个投影仪),还因为正在使用光电二极管来检测颜色的变化矩形。
答案 0 :(得分:1)
只需使用公共变量,属性或方法来更改颜色。
public Color slColor;
public static void GUIDrawRect(Color color) {
// draw rect
}
public void OnGUI() {
GUIDrawRect(slColor);
}
现在,您只需将变量slColor
设置为任何脚本或方法所需的颜色即可。