Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Trouble with EditorGUI.Popup updating

Discussion in 'Immediate Mode GUI (IMGUI)' started by bgsulz, Jul 27, 2021.

  1. bgsulz

    bgsulz

    Joined:
    Jan 17, 2019
    Posts:
    2
    I am attempting to create a custom property drawer for Unity object references that eliminates the need to drag and drop component references. The idea is that the property will be split into a standard field for the object reference and a popup from which the user can select an object reference, like so:

    2021-07-27_171353_LI.jpg

    I am running into issues updating both the dropdown and the property correctly. Here is my OnGUI method:

    Code (CSharp):
    1. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2.         {
    3.             var list = Objects(property, GetAttribute.GetterSource);
    4.            
    5.             EditorGUI.BeginProperty(position, label, property);
    6.  
    7.             if (list.Length <= 0)
    8.             {
    9.                 // Handle this case. Not important.
    10.             }
    11.  
    12.             var listNames = AsNames(list).ToArray(); // Converts the names to strings. This does not affect the contents of list.
    13.  
    14.             EditorGUI.BeginChangeCheck();
    15.             _index = EditorGUI.Popup(RightSide(position), _index, listNames); // _index is a private int. LeftSide and RightSide return the left and right halves of the Rect parameter.
    16.            
    17.             if (EditorGUI.EndChangeCheck())
    18.             {
    19.                 Undo.RecordObject(property.serializedObject.targetObject, $"Set index to {_index}");
    20.                 property.serializedObject.Update();
    21.                 property.objectReferenceValue = list[_index];
    22.             }
    23.  
    24.             GUI.enabled = false;
    25.             GUI.backgroundColor = property.objectReferenceValue == null ? Color.red : Color.white;
    26.            
    27.             EditorGUI.PropertyField(LeftSide(position), property, new GUIContent(GetterLabel(label)));
    28.             property.serializedObject.ApplyModifiedProperties();
    29.  
    30.             EditorGUI.EndProperty();
    31.         }
    As it stands now, the object reference updates accurately, but the popup does not.

    Moving
    property.serializedObject.Update();
    down one line--that is, after
    property.objectReferenceValue
    is updated--makes the popup update accurately, but prevents the object reference from updating at all.

    I am very new to editor scripting, so I would appreciate it immensely if you would point out my mistakes!

    If you are interested in testing the full code, you can find it here: Buggy PropertyDrawer - Pastebin.com

    Thank you so, so much in advance!