AudioClip PropertyDrawer - button rendered twice

Issue #12 resolved
Ivan Wang created an issue

Here's one of the more stranger bugs I've encountered:


The button is drawn once in an AudoClipPropertyDrawer, but shows up twice in the editor. Note that the "Element 0" text also takes up all the space, making it impossible to read the name of the AudioClip (but that's probably just part of the PropertyField?).

Steps to Repro

  1. Create the following classes:
  2. Create a GameObject and attach a SoundTest element.
  3. Observe two buttons in the editor.

Using Unity v5.5.0f3.

Comments (3)

  1. Tor Esa Vestergaard

    We've reproduced this bug, and introduced a fix which will be included in the next patch.

    The issue was that you called PropertyField with the same property that was currently being drawn, causing Unity to find your property drawer and draw it once again. Unity internally keeps track of a bunch of stuff to make sure PropertyField doesn't invoke the same drawer twice, hence why your drawer did not result in an infinite loop. We now set Unity's internal state properly before we invoke your drawer, to make sure this doesn't happen.

    Meanwhile, to fix the issue before you get the next patch distribution, you can change your drawer's OnGUI code to the following (using ObjectField instead of PropertyField to draw the AudioClip value):

    public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
        EditorGUI.BeginProperty(position, label, prop);
        if (prop.objectReferenceValue != null)
            float totalWidth = position.width;
            position.width = totalWidth - (totalWidth / 4);
            prop.objectReferenceValue = EditorGUI.ObjectField(position, label, prop.objectReferenceValue, typeof(AudioClip), false);
            position.x += position.width;
            position.width = totalWidth / 4;
            GUI.Button(position, "test");
             prop.objectReferenceValue = EditorGUI.ObjectField(position, label, prop.objectReferenceValue, typeof(AudioClip), false);
  2. Log in to comment