这是单例的另一种写法,不需要提前将其布置到场景中
using UnityEngine;using System;public class GlobalMonoEvents : MonoBehaviour{ public static UnityEngine.Events.UnityEvent OnXXX;//举例,一个事件广播 [RuntimeInitializeOnLoadMethod] //游戏一启动时调用的静态方法 public static void Initialize(){ var gameObj = new GameObject("GlobalMonoEvents"); gameObj.AddComponent<GlobalMonoEvents>(); DontDestroyOnLoad(gameObj); }}