单例模式分为饿汉式和懒汉式,
顾名思义,
unity由于其特性,可以快捷的一个单例
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Global : MonoBehaviour { public static Global instance; static Global() { GameObject go = new GameObject("#Globa#"); //在进程中不被销毁 DontDestroyOnLoad(go); instance = go.AddComponent<Global>(); } }