How can I color a PrefixLabel in Unity Editor?

南楼画角 提交于 2019-12-13 03:57:00

问题


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:


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;


来源:https://stackoverflow.com/questions/51397298/how-can-i-color-a-prefixlabel-in-unity-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!