【问题标题】:How can I color a PrefixLabel in Unity Editor?如何在 Unity 编辑器中为 PrefixLabel 着色?
【发布时间】:2018-07-18 08:37:36
【问题描述】:

我的问题很简单:我只想能够使用EditorGUILayout.PrefixLabel,但将文本颜色更改为白色。

但到目前为止我还没有运气。 我可以轻松地更改所有其他元素的颜色,但对于 PrefixLabel 没有任何效果。 我想坚持使用PrefixLabel,因为将所有标签和字段安排得井井有条的代码更少。

到目前为止我尝试过的一些事情:

使用EditorStyles.label.normal.textColor

var old = EditorStyles.label.normal.textColor;
EditorStyles.label.normal.textColor = Color.white;
EditorGUILayout.PrefixLabel("label Text");
EditorStyles.label.normal.textColor = old;

另外申请new GUIStyle(EditorStyles.label)

var old = EditorStyles.label.normal.textColor;
EditorStyles.label.normal.textColor = Color.white;
EditorGUILayout.PrefixLabel("label Text", new GUIStyle(EditorStyles.label));
EditorStyles.label.normal.textColor = old;

直接使用EditorStyles.whiteLabel

EditorGUILayout.PrefixLabel("label Text", new GUIStyle(EditorStyles.whiteLabel));

使用GUI.contentColor

var old = GUI.contentColor;
EditorStyles.label.normal.textColor = Color.white;
EditorGUILayout.PrefixLabel("label Text");
GUI.contentColor = old;

使用GUI.skin.label.normal.textColor

var old = GUI.skin.label.normal.textColor;
GUI.skin.label.normal.textColor = Color.white;
EditorGUILayout.PrefixLabel("label Text");
GUI.skin.label.normal.textColor = old;

使用new GUIStyle

whiteTextStyle = new GUIStyle(EditorStyles.label) 
{ 
    normal = { 
        textColor = Color.white;
    } 
};
EditorGUILayout.PrefixLabel("label Text", whiteTextStyle);

任何提示我还能尝试什么?

【问题讨论】:

    标签: unity3d customization unity3d-editor unity-editor


    【解决方案1】:

    EditorStyles.label.normal.textColor 已经可以工作了,但是我不得不在EditorGUILayout.XYField 之后重新设置颜色,因为在现场调用之前没有绘制 PrefixLabel。

    var old = EditorStyles.label.normal.textColor;
    EditorStyles.label.normal.textColor = Color.white;
    EditorGUILayout.PrefixLabel("label Text");
    
    EditorGUILayout.IntField(5);
    
    EditorStyles.label.normal.textColor = old;
    

    【讨论】:

      猜你喜欢
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多