Search Unity

issue with deleting unity event listener when drawn in custom inspector

Discussion in 'Immediate Mode GUI (IMGUI)' started by mrSaig, Jan 26, 2015.

  1. mrSaig

    mrSaig

    Joined:
    Jan 14, 2010
    Posts:
    68
    hey Guys,

    you all know and love UnityEvents :)
    I used them in my Editor extension but i just realized i can't properly delete previously entered EventListener when i draw them in the Custom inspector.
    Adding changing stuff no problem but deleting them is.
    When removing them from the list of listeners they appear again when the project gets reserialized (enter/leave played or when recompiling the solution)


    Here's what i did:

    The Test monobehviour:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using System.Collections;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.  
    8.     public UnityEvent onSomething;
    9. }
    10.  
    Custom editor for monobehaviour:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(Test))]
    6. public class TestEditor : Editor
    7. {
    8.  
    9.    
    10.     public override void OnInspectorGUI()
    11.     {
    12.         SerializedObject sobj = new SerializedObject((Test)target);
    13.         SerializedProperty prop = sobj.FindProperty("onSomething");
    14.         EditorGUILayout.PropertyField(prop);
    15.         sobj.ApplyModifiedProperties();
    16.     }
    17. }
    18.  

    Result:

    When you add some listeners to the unityevent via the custom inspector all is good
    But when you try to remove a listener and the project gets serialized (play/stop or changin code and let unity recompile) the listener appears again.
    No way to delete it appearently!

    Any tipps? am I missing something? Or is this a bug on unity side?

    Cheers
    mrSaIG
     
  2. russcat

    russcat

    Joined:
    Feb 12, 2013
    Posts:
    6
    When I changed this line:

    SerializedObject sobj =new SerializedObject((Test)target);

    To this:
    SerializedObject sobj = serializedObject;

    It appears to work correctly. A bug in the SerializedObject constructor?