总结学习的一些Unity常用的设计模式,还在不断学习补充中...
- 单例模式
using UnityEngine; public class MonoSingleton: MonoBehaviour { protected static MonoSingleton s_Instance; public static MonoSingleton Instance => s_Instance; private void Awake() { if (s_Instance != null) { Destroy(gameObject); } else { s_Instance = this; } } private void OnDestroy() { if (s_Instance == this) { s_Instance = null; } } } -
泛型单例模式using UnityEngine; public class MonoSingleton
: MonoBehaviour where T : MonoSingleton { protected static T s_Instance; public static T Instance => s_Instance; public static bool IsInitiated => s_Instance != null; protected virtual void Awake() { if (s_Instance != null) { Destroy(gameObject); } else { s_Instance = (T) this; } } protected virtual void OnDestroy() { if (s_Instance == this) { s_Instance = null; } } } -
观察者模式 - MVC模式
- 工厂模式
- 享元模式



