Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Declaring events?

Discussion in 'UGUI & TextMesh Pro' started by SunnySunshine, Oct 18, 2014.

  1. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Is it possible to create custom events that uses Unity 4.6 system for selecting listeners and methods in the inspector?

     
  2. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    yes use public UnityEvent field in script and call Invoke() (in script or this will not work) whenever u want.
     
    Last edited: Oct 18, 2014
  3. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Awesome.

    Is there a way to declare the event in such a way that passing arguments works?
     
  4. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    this is possible but for more infos use off/online scripting reference this is good enough
     
  5. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Having trouble finding things in the in the 4.6 documentation. Many things don't even seem to be linked, even though they're there (only find through google).

    Can you link where in the documentation it shows how to create custom events with parameters allowed?
     
  6. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    SunnySunshine likes this.
  7. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    To have parameters you need to inherit from UnityEvent<T> like so

    Code (Custom UnityEvent):
    1. public MyEventType onMyEvent;
    2.  
    3. [System.Serializable]
    4. public class MyEventType : UnityEvent<float> {}
    I learned this from looking through the source code of the Trigger component.
    https://gist.github.com/stramit/d473dec2a6fe7dd6fb61

    Code (Toggle):
    1. [Serializable]
    2. public class ToggleEvent : UnityEvent<bool>
    3. {}
    You can add up to 4 parameters.
     
    SunnySunshine likes this.
  8. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Yes, I looked through the JS example in the documentation that Breyer linked, and understood the implication.

    I think it's unfortunate that you have to extend a class. It would have been easier to just declare for example UnityEvent<float>, and let Unity handle the creation of any additional classes required behind the scenes. If custom behavior is required then obviously you'd have to extend the class, but I bet that 95% of the time all you'd want to do is simply have the standard unity event but with a specific type parameter.
     
  9. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    Yea it's a bit odd. I'm sure it has to do with some internal limitations or something. They allow you to define List<T> without having to inherit from a class so why not UnityEvent<T> too?
     
  10. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    how would i display this when i got a editor script and custom inspector? got it all working perfect with the typeless UnityEvent, i just dont know how to make it display in a custom inspector as it does without one.
     
  11. Jake-L

    Jake-L

    Joined:
    Oct 17, 2009
    Posts:
    397
    Good question. As soon as I use UnityEvent<MyEvent> the field hides from the inspector:

    Code (csharp):
    1. [Serializable]
    2. public class MyEvent : UnityEvent<float> {}
    Anyone got this working?
     
  12. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    That's just declaring the class. You need to declare a reference to your custom event too.

    Code (CSharp):
    1. [Serializable]
    2. public class MyEvent : UnityEvent<float> {}
    3.  
    4. public MyEvent onMyEvent;
     
  13. Jake-L

    Jake-L

    Joined:
    Oct 17, 2009
    Posts:
    397
    Yes, had those reference, but got another error. Thanks, it's working now!