简单的事件系统在Unity [英] Simple event system in Unity

查看:285
本文介绍了简单的事件系统在Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的事件系统在Unity(C#的方式),但我有实现它的问题。

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

    }
}

到目前为止没有问题,但我不知道我该怎么设计这个,所以特定类型的游戏对象的任何,将认购本次活动。例如,如果你有GO的工作人员类别,即工作8比6,并在运行时的实例化;我要订阅的主类这一类的,或者我应该创建一个脚本,我到游戏的对象prefab,订阅该事件?

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?

我们的目标是,所有的工作人员应该在运动中心做了8比6的转变,知道什么时候该一天结束事件火了起来,但我并不想订阅的每一个prefab我实例化。

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.

从我的理解,我应该附加一个脚本在prefab所需的code,但我无法找到证明,如果这是在事实上做到这一点的方式一个实际的例子。

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.

推荐答案

您必须使用普通的当今时代 UnityEvent

You must use ordinary UnityEvent of the modern era.

这是非常简单,再简单不过了。

It is perfectly simple, couldn't be easier.

请这样BigScript.cs脚本

Make a script like this BigScript.cs

using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class BigScript:MonoBehaviour
    {
    [Header("Here's a cool event!")]
    public UnityEvent whoa;
    }

把它放在一个游戏对象。现在看它的督察。酷吧?

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

简单

其它脚本到那里做时,哇发生事情发生。

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

在这里输入的形象描述

就这么简单。

当你想调用在脚本的事件,它只是这个

When you want to call the event in that script, it is just this

private void YourFunction()
    {
    whoa.Invoke();
    }

(如果你preFER,如果(哇= NULL)whoa.Invoke(!);

如果你是一个新的编程爱好者

If you are a new hobbyist programmer

https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system

在罕见的情况下,可能需要通过code以添加一个侦听器。 (我的意思,而不是简单地拖动它,编辑器。)如果是这样,它只是

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

whoa.AddListener(ScreenMaybeChanged);

需要明确的是 - 通常决不再这样,只需将要连接的我只提到它的完整性。

TO BE CLEAR - NORMALLY NEVER DO THAT, SIMPLY DRAG TO CONNECT. I only mention it for completeness.

这是所有有给它。

这篇关于简单的事件系统在Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆