Search Unity

Event Manager System

Discussion in 'Scripting' started by kabuto178, May 29, 2017.

  1. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    Is it possible to send a custom object as a parameter of a UnityAction? In my event manager system I have
    UnityAction eventListener = new UnityAction(func);

    void func(int a){
    //do summin with passed int a
    }

    I am not sure how to implement this in my current system but I do have it working without the parameters though.
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    UnityAction (and its Generic types) are just delegate declaractions. You can use UnityEvent to pass data around. you'll want to define a new class that has your custom class as its generic type.

    Code (CSharp):
    1. [System.Serializable]public class MyCustomClass{}
    2.  
    3. public class CustomEvent : UnityEvent<MyCustomClass>{}
    4.  
    5. public class ExampleScript : MonoBehaviour
    6. {
    7.     [HideInInspector]public CustomEvent OnEvent = new CustomEvent();
    8.  
    9.     private void OnEnable()
    10.     {
    11.         OnEvent.AddListener(MyHandler);
    12.  
    13.         OnEvent.Invoke(new MyCustomClass());
    14.     }
    15.  
    16.     private void OnDisable()
    17.     {
    18.         OnEvent.RemoveListener(MyHandler);
    19.     }
    20.  
    21.     private void MyHandler(MyCustomClass data)
    22.     {
    23.         Debug.Log("Got the Data!");
    24.     }
    25. }
    If the type was a simple, serializeable type like int string float bool or UnityEngine.Object then you could link up invocations for that event in the inspector. However for custom classes, while the event will still work just fine it has very little support for you in the inspector (hence why I tagged it with [HideInInspector] in the example to avoid confusion). All it really means is that you just have to set it up via code.
     
  3. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    I would want it to invoke a generic type so as not to be stuck with only one type of passing parameter.
     
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    then make the event pass the most abstract type needed, the handlers can then try to cast it to specific types when needed. if it can't cast then they can do nothing (return), or you can have so that depending on the type it does different things (like if a Collider is a Sphere Collider do one thing vs Box Collider do another).
     
  5. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    I was trying to implement it to pass data between scripts without having to find gameobject or linking the scripts directly
     
  6. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    I typically do this via ScriptableObject Events. You can find an example of this on this post. The code is written in a way so that one Monobehaviour its not coupled to another, yet instances can be linked up in the inspector (and yes this will also work on custom classes). And it also works with linking up two separate prefabs (as gone over in that thread).
     
  7. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    Ok, I will take a look now and see. Thanks for the tips