Home > Enterprise >  How to pass a input parameter of a UnityEvent to its listener in the inspector
How to pass a input parameter of a UnityEvent to its listener in the inspector

Time:09-05

So, I have a UnityEvent<int> declared like this:

public UnityEvent<int> onValueChanged;

and inside the Unity inspector, i want to pass the integer parameter of the event to its listener like in this picture. Is there a way to do this? I know it is possible to do in code using delegates but is there a way to do it only in the inspector?

Thanks in advance

CodePudding user response:

Unity cannot serialize generics properly (except for List<T> and arrays). That's why you can't see it in the inspector.

If you want to expose a generic parameter, you have to create a type for it. It doesn't even require any code, the type itself is enough.

[Serializable]
public class IntUnityEvent : UnityEvent<int>
{
}

public class YourClass : MonoBehaviour
{
    public IntUnityEvent ExposedEvent;
}

I should add that you have could just open the documentation, as Unity tells you exactly the same on their documentation page for UnityEvent<T0>.

  • Related