Unity3D-ExtendedEvent icon indicating copy to clipboard operation
Unity3D-ExtendedEvent copied to clipboard

Can't added ExtendedEvent to a Serialized Object

Open malitvin opened this issue 8 years ago • 2 comments

When you add an Extended Event to a non MonoBehaviour class there will be a non reference exception on line 22 of ExtendedEventPropertyDrawer

The value field is null capture

malitvin avatar Mar 09 '17 22:03 malitvin

yeah same here image

friuns2 avatar Feb 16 '18 09:02 friuns2

if you need to fix this use those methods



    private void RestoreState( SerializedProperty property ) {
        if ( rList == null || eEvent == null ) {

            
            header = property.name;
            //var target = property.serializedObject.targetObject;
            //eEvent = fieldInfo.GetValue( target ) as ExtendedEvent;
            object o = GetTargetObjectOfProperty(property);
            eEvent = o as ExtendedEvent;
            foreach ( var item in eEvent.Listeners ) {
                item.Initialize();
            }

            rList = new ReorderableList( eEvent.Listeners, typeof( ExtendedEvent.GameObjectContainer ) );
            rList.draggable = false;
            rList.elementHeight *= 2;
            rList.drawHeaderCallback = DrawHeaderInternal;
            rList.drawElementCallback = DrawElementInternal;
            rList.onAddCallback = AddInternal;
            rList.onRemoveCallback = RemoveInternal;
        }
    }

    private static object GetValue_Imp(object source, string name, int index)
    {
        var enumerable = GetValue_Imp(source, name) as System.Collections.IEnumerable;
        if (enumerable == null) return null;
        var enm = enumerable.GetEnumerator();
        //while (index-- >= 0)
        //    enm.MoveNext();
        //return enm.Current;

        for (int i = 0; i <= index; i++)
        {
            if (!enm.MoveNext()) return null;
        }
        return enm.Current;
    }


    private static object GetValue_Imp(object source, string name)
    {
        if (source == null)
            return null;
        var type = source.GetType();

        while (type != null)
        {
            var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            if (f != null)
                return f.GetValue(source);

            var p = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
            if (p != null)
                return p.GetValue(source, null);

            type = type.BaseType;
        }
        return null;
    }

    public static object GetTargetObjectOfProperty(SerializedProperty prop)
    {
        var path = prop.propertyPath.Replace(".Array.data[", "[");
        object obj = prop.serializedObject.targetObject;
        var elements = path.Split('.');
        foreach (var element in elements)
        {
            if (element.Contains("["))
            {
                var elementName = element.Substring(0, element.IndexOf("["));
                var index = System.Convert.ToInt32(element.Substring(element.IndexOf("[")).Replace("[", "").Replace("]", ""));
                obj = GetValue_Imp(obj, elementName, index);
            }
            else
            {
                obj = GetValue_Imp(obj, element);
            }
        }
        return obj;
    }

friuns2 avatar Feb 16 '18 09:02 friuns2