Search code examples
unity-game-engineunity-editor

Unity editor - How to stop field from turning blue when its edited


I am making a tool in Unity to build your project for muliple platforms when you press a button.

I started with the preferences window for the tool, and came up with an anoying thing. Whenever I change the enum value of the EnumPopup field, the field turns blue in the editor window. Is there a way to disable this?

This is the code I am using to show the enum field on the window

This is the window when the enum field has not been changed

This is the window when the enum field has been changed

See how in the 2nd picture the field is not blue, and in the 3rd picture the field has changed to blue? How do I prevent this from happening?

Thanks in advance!


Solution

  • Difficult to help without having the rest of your code.

    This is Unity built-in behaviour. I tried a lot of stuff see here to disable / overwrite the built-in coloring of prefix labels but had no luck so far.


    A workarround however might be to instead use an independent EditorGUI.LabelField which will not be affected by the EnumPopup together with the EditorGUIUtility.labelWidth:

    var LabelRect = new Rect(
        FILEMANAGEMENT_ENUMFIELD_RECT.x, 
        FILEMANAGEMENT_ENUMFIELD_RECT.y,
        // use the current label width
        EditorGUIUtility.labelWidth, 
        FILEMANAGEMENT_ENUMFIELD_RECT.height
    );
    
    var EnumRect = new Rect(
        FILEMANAGEMENT_ENUMFIELD_RECT.x + EditorGUIUtility.labelWidth, 
        FILEMANAGEMENT_ENUMFIELD_RECT.y, 
        FILEMANAGEMENT_ENUMFIELD_RECT.width - EditorGUIUtility.labelWidth, 
        FILEMANAGEMENT_ENUMFIELD_RECT.height
    );
    
    EditorGUI.LabelField(LabelRect, "File relative to");
    QuickBuilder.Settings.Relation = (QuickBuilder.Settings.PathRelation)EditorGUI.EnumPopup(EnumRect, QuickBuilder.Settings.Relation);
    

    As you can see the label is not turned blue while the width keeps being flexible

    enter image description here


    Sidenotes
    Instead of setting values via edito scripts directly like QuickBuilder.Settings.Relation = you should always try and use the proper SerializedProperty. It handles things like Undo/Redo and also marks the according objects and scenes as dirty.

    Is there also a special reason why you use EditorGUI instead of EditorGUILayout? In the latter you don't need to setup Rects.

    EditorGUILayout.BeginHorizontal();
    {
        EditorGUILayout.LabelField("File relative to", GUILayout.Width(EditorGUIUtility.labelWidth));
        QuickBuilder.Settings.Relation = (QuickBuilder.Settings.PathRelation)EditorGUILayout.EnumPopup(QuickBuilder.Settings.Relation);
    }
    EditorGUILayout.EndHorizontal();