Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Nested UnityEvent subclass inside of a Generic class.

Discussion in 'Scripting' started by conziztent, Sep 23, 2019.

  1. conziztent

    conziztent

    Joined:
    Aug 13, 2019
    Posts:
    4
    Code (CSharp):
    1. public class BaseClass<T> : ScriptableObject
    2. {
    3.     [System.Serializable]
    4.     private class UnityEventType : UnityEvent<T> {}
    5.  
    6.     [SerializeField]
    7.     private UnityEventType m_event;
    8. }
    9.  
    10. [CreateAssetMenu]
    11. public class SubclassBool : BaseClass<bool> {}
    The m_event field is not shown in the inspector. I can define a separate UnityEvent subclass for every use case subclass manually, and it will work, but I don't want to have such boilerplate code as I would have many subclasses.

    My question is: Why this doesn't work?
     
    TheHeftyCoder likes this.
  2. CMC-Kiesel_Markus

    CMC-Kiesel_Markus

    Joined:
    Oct 31, 2016
    Posts:
    9
    Hey there,

    I also had the same problem. A solution I came up with that reduces the boilerplate is:

    Code (CSharp):
    1.  
    2. public class GenericEvent<TType, TEvent> : MonoBehaviour where TEvent : UnityEvent<TType>
    3. {
    4.     public TEvent MyUnityEvent;
    5. }
    6.  
    7. [Serializable]
    8. public class IntUnityEvent : UnityEvent<int> {}
    9.  
    10. public class MyTypedEvent : GenericEvent<int, IntUnityEvent> { }
    11.