How can I color a PrefixLabel in Unity Editor?

2019-08-16 02:06发布

My question is that simple: I just want to be able to use EditorGUILayout.PrefixLabel but change the text color to white.

But so far I have no luck. I can easely change the color of all other elements but for the PrefixLabel nothing is working. I want to stick with PrefixLabel since it simply is less code to have all labels and fields arranged well.

A fiew things I tried so far:

using 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;

additionally applying 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;

directly using EditorStyles.whiteLabel

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

using GUI.contentColor

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

using 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;

using a new GUIStyle

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

Any hint what else I can try?

1条回答
Fickle 薄情
2楼-- · 2019-08-16 02:37

EditorStyles.label.normal.textColor would already have worked but I had to reset the color back after the EditorGUILayout.XYField because the PrefixLabel was not drawed until the field call.

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

EditorGUILayout.IntField(5);

EditorStyles.label.normal.textColor = old;
查看更多
登录 后发表回答