Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

c# - Simple event system in Unity

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must use UnityEvent.

public UnityEvent whoa;

It is dead easy.

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.

You will see the "whoa" event.

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

enter image description here

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

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

In rare cases you may need to add a listener via code rather than simply dragging it in the Editor. This is trivial:

whoa.AddListener(ScreenMaybeChanged);

(Normally you should never have to do that. Simply drag to connect. I only mention this for completeness.)


That's all there is to it.

Be aware of out of date example code regarding this topic on the internet.


The above shows how to connect simple functions that have no argument.

If you do need an argument:

Here is an example where the function has ONE FLOAT argument:

Simply add THIS CODE at the top of the file:

[System.Serializable] public class _UnityEventFloat:UnityEvent<float> {}

then proceed as normal. It's that easy.

// ...
using UnityEngine.Events;

// add this magic line of code up top...
[System.Serializable] public class _UnityEventFloat:UnityEvent<float> {}

public class SomeThingHappens : MonoBehaviour
{
    // now proceed as usual:
    public _UnityEventFloat changedLength;
    
    void ProcessValues(float v)
    {
        // ...
        changedLength.Invoke(1.4455f);
    }
}

When you drag from your other function to the Editor slot on this function:

Be certain to use the section - "Dynamic float".

enter image description here

(Confusingly your function will also be listed in the "Static Parameters" section! That is a huge gotchya in Unity!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...