提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
我们在开发Unity程序的过程中会用到事件系统,除了自己开发一套监听与广播系统,也可以使用Unity中原生的监听与广播系统——UnityEvent
Unity官方文档地址:https://docs.unity3d.com/cn/2019.4/ScriptReference/Events.UnityEvent.html
一、示例代码
首先要定义所有的事件,可以写一个事件类
Events.cs
using UnityEngine.Events;
[System.Serializable]
public class Events
{
public UnityEvent zeroParaEvent;
public UnityEvent<int> oneParaEvent;
public UnityEvent<int, string> twoParaEvent;
public UnityEvent<int, string, float> threeParaEvent;
}
然后是事件广播类,在这里触发相应的广播事件
Menu.cs
using UnityEngine;
using UnityEngine.UI;
public class Menu : MonoBehaviour
{
public Button zeroBt;
public Button oneBt;
public Button twoBt;
public Button threeBt

本文详细介绍了Unity内置的UnityEvent事件监听与广播系统,通过示例代码演示了如何定义事件、触发事件以及监听事件的实现。包括零参数、单参数、双参数和三参数事件的使用,并展示了如何在脚本中挂载和测试这些事件。

1万+

被折叠的 条评论
为什么被折叠?



