Serialize Generic Unity Event Inside of a Generic C# Class

I need to serialize the single parameter generic UnityEvent class when nested inside of a Generic C# object

This is the C# object:

[Serializable]
    public class AutoProperty<T>
    {
        [SerializeField]
        private T m_Data;

        [Serializable]
        public class UnityEventGeneric : UnityEvent<T> { }

        public UnityEventGeneric onChangeEvent = new UnityEventGeneric();

        public T value
        {
            get { return m_Data; }
            set
            {
                m_Data = value;

                if (onChangeEvent != null)
                    onChangeEvent.Invoke(value);
            }
        }
    }

I already have a wrapper for the different types I intend on using the most:

[Serializable]
    public class AutoPropertyString : AutoProperty<string> { }

    [Serializable]
    public class AutoPropertyInt : AutoProperty<int> { }
    [Serializable]
    public class AutoPropertyFloat : AutoProperty<float> { }

    [Serializable]
    public class AutoPropertyVector2 : AutoProperty<Vector2> { }
    [Serializable]
    public class AutoPropertyVector3 : AutoProperty<Vector3> { }

and these serialize the variable ‘m_Data’ properly. It’s just the event that doesn’t. I assume it has something to do with the fact that it is a generic event inside of a generic class, but I was hoping I could still somehow get this to serialize properly.

Any help is greatly appreciated.

Yes the problem here is that the serializer cannot serialize a generic class. So in this example, it can serialize AutoPropertyString, and it could even serialize a field like T defaultValue defined on AutoProperty<T> (provided that the generic type argument passed as T on your subclass is something Unity can serialize).

In this case, I would instead do something like make AutoProperty<T> abstract and define a method on it like abstract void OnValueChanged(T newValue). Each concrete subclass would need to define its own event class (ugh), serialize a field of that type, and handle invocation inside that method. I would then add an interface on the AutoProperty<T> for registering/deregistering event handlers if needed, which I would then of course implement in the subclasses.