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

查看:198
本文介绍了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

    }
}

到目前为止没有问题,但我不知道如何设计,所以任何GameObject的特定类型,将订阅此事件。例如,如果你有一个GO的工作人员类别,那么工作在8到6,你在运行时实例化;我应该在这个类别的主类中订阅,还是应该创建一个我附加到游戏对象预制的脚本,那个订阅该事件?

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班,知道日子完成事件何时发生,但是我不想订阅我实例化的每一个预制。

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.

从我的理解,我应该附加一个脚本与所需的代码在预制,但我找不到一个实际的例子,显示,这实际上是这样做的方式。 / p>

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

    public UnityEvent whoa;

这不容易。使用UnityEngine制作脚本BigScript.cs

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?

很简单。要在BigScript中调用该事件,它只是

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(whoa!= null)whoa.Invoke();

如果你是新的:优秀的Unity Events视频教程

在极少数情况下,您可能需要通过代码添加一个监听器。 (我的意思是,而不是简单地在编辑器中拖动它。)

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);

我只提到它的完整性。

这就是它的所有。

Unity事件是Unity如何令人难以置信的一个惊人的例子。永远记住,如果您正在做一些复杂的工作,那么在使用Unity时,您会错误的做到这一点。这是小孩戏。

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.

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

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