Simple event system in Unity

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I am trying to use the event system in Unity (the C# way), but I am having problems to implement it.

Most of the examples, show one class, where you define the handler; then you write in the same class, the function that match the signature of the handler, and you write the events as static

public class EventExample : MonoBehaviour {     public delegate void ExampleEventHandler();     public static event ExampleEventHandler OneDayPassed;      public static void NextTurn()     {         // do stuff then send event         if (OneDayPassed != null)             OneDayPassed();     } } 

Then in another class, you subscribe to the events, creating a function that actually do something

public class EntityWatcher: MonoBehaviour {     void Start()     {         EventExample.OneDayPassed += this.PrepareNextDay;     }      public void PrepareNextDay()     {         // Do other stuff that happen after the day is done      } } 

So far no problems, but I am not sure how do I design this, so any GameObject of a particular type, will subscribe to this event. For example, if you have a staff category of GO, that is working 8 to 6, and you instantiate them at runtime; should I subscribe in the main class of this category, or should I create a script that I attach to the Game Object prefab, that subscribe to the event?

The objective is that all the staff members that should do a 8 to 6 shift at the sport center, know when the "day is done" event fire up, but I don't want to subscribe every single prefab that I instantiate.

From my understanding, I should attach a script with the needed code on the prefab, but I can't find a practical example that show if that is in fact the way to do it.

回答1:

You must use UnityEvent.

    public UnityEvent whoa; 

It could not be easier. Make a script BigScript.cs

using UnityEngine; using System.Collections; using UnityEngine.Events;  public class BigScript:MonoBehaviour     {     [Header("Here's a cool event! Drag anything here!")]     public UnityEvent whoa;     } 

Put it on a game object. Now look at it in the Inspector. Cool right?

Simply drag your other scripts to there, to make something happen, when "whoa" happens.

It's that simple. To call the event in BigScript, it is just

public class BigScript:MonoBehaviour     {     [Header("Here's a cool event! Drag anything here!")]     public UnityEvent whoa;      private void YourFunction()       {       whoa.Invoke();       } 

(if you prefer, if (whoa!=null) whoa.Invoke(); )


If you're new: excellent Unity Events video tutorial.


In rare cases you may need to add a listener via code. (I mean, rather than simply dragging it in the Editor.)

whoa.AddListener(ScreenMaybeChanged); 

To be clear: never do that, simply drag to connect. I only mention it for completeness.


That's all there is to it.

Unity Events are an amazing example of how Unity make it ridiculously easy. Always remember that when working with Unity if you're doing something complicated, you're doing it the wrong way. It's child's play.



回答2:

Don't worry too much about the number of subscribers if you are only talking about a few dozens. Yes, once you get into the hundreds or thousands it might be a good idea to optimize this, but even then. First rule of optimization: don't optimize.

So: just have every instance subscribe to the event and be done with it.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!