C#/Unity3D 单例模板(单例属性模板)

C# 单例单例属性

不做过多解释,非面向大众

csharp 复制代码
using System;
namespace EasyAVG
{
    public static class SingletonProperty<T> where T : class
    {

        private static readonly object locker = new object();
        private volatile static T instance = null;
        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = Activator.CreateInstance<T>();
                        }
                    }
                }
                return instance;
            }
        }

        public static void Unload() => instance = null;
        public static void Reload() => instance = Activator.CreateInstance<T>();
    }

}
csharp 复制代码
namespace EasyAVG
{
    public abstract class Singleton<T> where T : Singleton<T>
    {
        public static T Instance => SingletonProperty<T>.Instance;
        protected Singleton() { }


        public static void Unload() => SingletonProperty<T>.Unload();
        public static void Reload() => SingletonProperty<T>.Reload();
    }

}

Unity Mono单例 /单例属性模板

csharp 复制代码
using UnityEngine;
namespace EasyAVG
{
    public static class MonoSingletonProperty<T> where T : MonoBehaviour
    {

        private static T instance = null;
        public static T Instance
        {
            get
            {
                if (instance == null) InitializeAfterSceneLoad();
                return instance;
            }
        }

        public static void InitializeAfterSceneLoad()
        {
            if (instance != null) return;
            GameObject container = new GameObject(typeof(T).Name);
            UnityEngine.Object.DontDestroyOnLoad(container);
            instance = container.AddComponent<T>();
        }

        public static void Unload()
        {
            UnityEngine.Object.Destroy(instance.gameObject);
            instance = null;
        }
        public static void Reload()
        {
            Unload();
            InitializeAfterSceneLoad();
        }
    }

}
csharp 复制代码
using UnityEngine;


namespace EasyAVG
{
    public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {

        protected MonoSingleton() { }
        public static T Instance => MonoSingletonProperty<T>.Instance;


        public static void SingletonInitializeAfterSceneLoad() => MonoSingletonProperty<T>.InitializeAfterSceneLoad();

        public static void Unload() => MonoSingletonProperty<T>.Unload();
        public static void Reload() => MonoSingletonProperty<T>.Reload();
    }
}
相关推荐
KookeeyLena810 分钟前
如何限制任何爬虫爬取网站的图片
开发语言·c++·爬虫
yanyanwenmeng29 分钟前
matlab基础
开发语言·算法·matlab
末央&1 小时前
【C++】内存管理
java·开发语言·c++
不是仙人的闲人1 小时前
Qt日志输出及QsLog日志库
开发语言·数据库·qt
八了个戒1 小时前
【TypeScript入坑】TypeScript 的复杂类型「Interface 接口、class类、Enum枚举、Generics泛型、类型断言」
开发语言·前端·javascript·面试·typescript
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机轨迹跟踪及避障Matlab代码
开发语言·matlab·无人机
Yan-英杰1 小时前
Encountered error while trying to install package.> lxml
开发语言·python·pandas·pip·issue
卡卡_R-Python1 小时前
海洋气象编程工具-Python
开发语言·python
爱学习的真真子1 小时前
菜鸟也能轻松上手的Java环境配置方法
java·开发语言
豆本-豆豆奶1 小时前
23个Python在自然语言处理中的应用实例
开发语言·python·自然语言处理·编程语音