Search Unity

UnityEvent, where have you been all my life?

Discussion in 'Scripting' started by JoeStrout, Apr 24, 2015.

?

Did you know about UnityEvent before this?

  1. No! Thank you! This is so cool!

    36.1%
  2. No, but I still don't see what's the big deal.

    5.1%
  3. Yes, but I didn't realize how cool they were.

    24.1%
  4. Yes, but I still don't see what's the big deal.

    12.3%
  5. Yes, and I use them all the time. Where have you been?!?

    22.4%
  1. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I wrote events like these way back in 2011 for my own projects. At first I thought they were the bees-knees and used them everywhere but after using them so long I've actually started to move away from them. I find that they are susceptible to bugs as a result of code that changes without you realising that an event was reliant on it. It's easy to delete a function or change its name or signature that was in use by one of these events and not get a compilation error as a result.

    They are great as an interface between UI buttons and code but beyond that they get tricky to maintain and debug. My new method of working is to instead write a small script that has a function that then hard codes what I use to put into events. You end up with a lot of helper functions in one but UIHelper script but at the end of the day it's much easier to keep track of and debug.

    Screen Shot 2018-07-23 at 10.07.54 AM.png
     
  2. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    Thank you guys, now the picture is more clear. I think I'm going to stick to plain old event delegates most of the time (with an exception of event manager which uses custom event system) and use UnityEvent for stuff like trigger volumes, when I want to wire up the logic within level itself. Or for interactive UI maybe.

    That is until I'll switch to UniRx...
     
  3. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    UnityEvent Drawbacks:
    1- It is slow (not important)
    2- You can not change functions (listeners) names easily, because you have to reassign them.
    3- You can not utilize interfaces, whenever you remove and add new component implementing the same interface, it again enforces you to reassign the functions. (couple tightly to the references)
    4- It is just able to serialize some predefined functions with specific parameters (static methods).
    But It is cool and can decouple listeners from publishers. Also, we do not need to write extra scripts as mediators between publishers and listeners. I sometimes apply unityEvents with scriptableObjects (GameEvents are SOs but listeners are monos) especially as listeners are independent of each other and can be executed with any orders.
     
    Last edited: Dec 30, 2018
  4. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    144
    Sorry if this is a bit of necromancy on the post but I feel I still need some clarification. I have been looking at UnityEvent using the UnityEngine.Events and while I see you can call events based off of colliders and triggers I am unsure how this can be used for tracking and linking scripts without requiring their game objects to "touch" one another.

    For example, I could use a UnityEvent with onCollisionEnter to see if a door was opened or closed based off of colliders at the closed point and the open point and another collider on the door that triggers these points as it swings between them. Or I could make a script that says one item is "attached" to another by having a collider on the attachable object that triggers with a collider on the attached object with onTriggerStay . These could report to a log that the door was opened or that the item was attached but how might I use a UnityEvent without triggers and colliders to report for example if a resource has been exhausted or a tutorial has been watched?

    It seems UnityEvents say if x touches y do z and that should cover what I want to do in many cases but I'm not seeing how to get the when x do y to see anything other then a box collider trigger or collision. Im more of an artist/noob developer then an engineer so what am I missing?
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Well if you want to use a UnityEvent during some other kind of event... you have to have a script that tracks that event.

    So like say you have a 'chest' script that when the player interacts with it an item is given to the player, and you want an event for when the chest is empty so for instance the chest can play a sound or change its material or any number of things you may want to do art wise. You might go about it this way:

    Code (csharp):
    1.  
    2. public class ChestScript : MonoBehaviour
    3. {
    4.  
    5.     public InventoryItem Item;
    6.     public int Quantity;
    7.     public UnityEvent OnExhausted = new UnityEvent();
    8.  
    9.     //this method might be called by the player by its 'interaction' script...
    10.     //I usually do this via implementing some IPlayerInteractable interface
    11.     public void OnPlayerInteracted(PlayerEntity player)
    12.     {
    13.         if (Quantity <= 0) return; //we're empty
    14.      
    15.         //we're implying that a player has an inventory of some sort, and that the inventory's
    16.         //AddItem method returns true if the item was successfully added
    17.         var inv = player.GetComponent<InventoryPouch>();
    18.         if(inv != null && inv.AddItem(Item))
    19.         {
    20.             Quantity--;
    21.             if(Quantity == 0)
    22.             {
    23.                 OnExhausted.Invoke();
    24.             }
    25.         }
    26.     }
    27.  
    28. }
    29.  
    You can call a UnityEvent at any time based on whatever rule you want. It doesn't have to necessarily be related to a trigger/collision event.
     
  6. ggpereira

    ggpereira

    Joined:
    Mar 30, 2015
    Posts:
    9
  7. EllaMilk

    EllaMilk

    Joined:
    Jan 11, 2021
    Posts:
    1
    Just take my knee, Dalao!