如何在 Unity 编辑器中为 PrefixLabel 着色?

How can I color a PrefixLabel in Unity Editor?

我的问题就是这么简单:我只想能够使用 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);

任何提示我还可以尝试什么?

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;