首页 > 解决方案 > 如何将所有 gui 元素(如标签或按钮)的文本颜色更改为白色?

问题描述

这是我想在我的项目中从另一个项目中获取的白色和文本/字体大小和样式的示例。

例子

我想进入编辑器窗口背景颜色和屏幕截图中的白色文本颜色。

我不需要树视图来制作这种文本。我正在使用 EditorWindow 类型的脚本。

在顶部我做了:

private static Texture2D tex;

然后:

    [MenuItem("Window/Test")]
        static void ShowEditor()
        {
            editor = EditorWindow.GetWindow<Test>();
            editor.Init();

            tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
            tex.SetPixel(0, 0, Color.black);
            tex.Apply();

            CenterWindow();
        }

And inside the OnGUI:

void OnGUI()
    {
        GUI.DrawTexture(new Rect(0, 0, maxSize.x, maxSize.y), tex, ScaleMode.StretchToFill);
        //GUI.Label(new Rect(200, 200, 100, 100), "A label");
        //GUI.TextField(new Rect(20, 20, 70, 30), "");

        GUIStyle itemStyle = new GUIStyle();  //make a new GUIStyle

        itemStyle.alignment = TextAnchor.MiddleLeft; //align text to the left
        itemStyle.active.background = itemStyle.normal.background;  //gets rid of button click background style.
        itemStyle.margin = new RectOffset(0, 0, 0, 0);
        GUI.backgroundColor = Color.white;
        GUI.skin.toggle.fontStyle = FontStyle.Normal;
        GUI.skin.toggle.fontSize = 13;
    }

但这并没有太大变化。它以黑色绘制和着色整个窗口,但 gui 元素的元素不像屏幕截图示例中那样为白色。

标签: c#unity3d

解决方案


你失踪了itemStyle.normal.textColor = Color.white;。添加这个应该会使所有具有这种样式的文本变成白色


推荐阅读