如何将Unity中泛型单例模式改写为继承自Mo的通用长尾?

2026-03-31 13:021阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计359个文字,预计阅读时间需要2分钟。

如何将Unity中泛型单例模式改写为继承自Mo的通用长尾?

单例模式是设计模式中最常见的一种,它不多做解释。但应尽量减少使用,一般全局管理类才使用单例。普通泛型单例:

javapublic abstract class SingletonT where T : class, new(){ private static T instance;}

单例模式是设计模式中最为常见的,不多解释了。但应该尽量避免使用,一般全局管理类才使用单例。

如何将Unity中泛型单例模式改写为继承自Mo的通用长尾?

普通泛型单例:

public abstract class Singleton<T> where T : class, new() { private static T instance = null; private static readonly object locker = new object(); public static T Instance { get { lock (locker) { if (instance == null) instance = new T(); return instance; } } } }

继承MonoBehaviour的泛型单例:

using UnityEngine; public abstract class MonoSingleton <T>: MonoBehaviour where T:MonoBehaviour { private static T instance = null; private static readonly object locker = new object(); private static bool bAppQuitting; public static T Instance { get { if (bAppQuitting) { instance = null; return instance; } lock (locker) { if (instance == null) { instance = FindObjectOfType<T>(); if (FindObjectsOfType<T>().Length > 1) { Debug.LogError("不应该存在多个单例!"); return instance; } if (instance == null) { var singleton = new GameObject(); instance = singleton.AddComponent<T>(); singleton.name = "(singleton)" + typeof(T); singleton.hideFlags = HideFlags.None; DontDestroyOnLoad(singleton); } else DontDestroyOnLoad(instance.gameObject); } instance.hideFlags = HideFlags.None; return instance; } } } private void Awake() { bAppQuitting = false; } private void OnDestroy() { bAppQuitting = true; } }

使用方法直接用类去继承这两个抽象单例即可,使用T.Instance就可以直接取得该类(T)的唯一实例了。

以上就是Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)的详细内容,更多关于unity单例设计模式的资料请关注易盾网络其它相关文章!

本文共计359个文字,预计阅读时间需要2分钟。

如何将Unity中泛型单例模式改写为继承自Mo的通用长尾?

单例模式是设计模式中最常见的一种,它不多做解释。但应尽量减少使用,一般全局管理类才使用单例。普通泛型单例:

javapublic abstract class SingletonT where T : class, new(){ private static T instance;}

单例模式是设计模式中最为常见的,不多解释了。但应该尽量避免使用,一般全局管理类才使用单例。

如何将Unity中泛型单例模式改写为继承自Mo的通用长尾?

普通泛型单例:

public abstract class Singleton<T> where T : class, new() { private static T instance = null; private static readonly object locker = new object(); public static T Instance { get { lock (locker) { if (instance == null) instance = new T(); return instance; } } } }

继承MonoBehaviour的泛型单例:

using UnityEngine; public abstract class MonoSingleton <T>: MonoBehaviour where T:MonoBehaviour { private static T instance = null; private static readonly object locker = new object(); private static bool bAppQuitting; public static T Instance { get { if (bAppQuitting) { instance = null; return instance; } lock (locker) { if (instance == null) { instance = FindObjectOfType<T>(); if (FindObjectsOfType<T>().Length > 1) { Debug.LogError("不应该存在多个单例!"); return instance; } if (instance == null) { var singleton = new GameObject(); instance = singleton.AddComponent<T>(); singleton.name = "(singleton)" + typeof(T); singleton.hideFlags = HideFlags.None; DontDestroyOnLoad(singleton); } else DontDestroyOnLoad(instance.gameObject); } instance.hideFlags = HideFlags.None; return instance; } } } private void Awake() { bAppQuitting = false; } private void OnDestroy() { bAppQuitting = true; } }

使用方法直接用类去继承这两个抽象单例即可,使用T.Instance就可以直接取得该类(T)的唯一实例了。

以上就是Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)的详细内容,更多关于unity单例设计模式的资料请关注易盾网络其它相关文章!