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();
    }
}
相关推荐
张人玉20 小时前
C#WPF如何实现登录页面跳转
ui·c#·wpf
无敌最俊朗@20 小时前
解决 QML 中使用 Qt Charts 崩溃的三个关键步骤
开发语言·qt
会飞的小新21 小时前
C 标准库之 <errno.h> 详解与深度解析
c语言·开发语言
张人玉21 小时前
C#WPF如何跳转页面
笔记·ui·c#·wpf
胡八一21 小时前
30 分钟上手 exp4j:在 Java 中安全、灵活地计算数学表达式
java·开发语言·安全
future_studio21 小时前
聊聊 Unity(小白专享、C# 小程序 之 自动更新)
unity·小程序·c#
主宰者21 小时前
【C#】.NET Framework 4.8环境下使用Sqlite的问题总结
sqlite·c#·.net
郝学胜-神的一滴1 天前
Linux 进程控制块(PCB)解析:深入理解进程管理机制
linux·服务器·开发语言
后端小张1 天前
【鸿蒙开发手册】重生之我要学习鸿蒙HarmonyOS开发
开发语言·学习·华为·架构·harmonyos·鸿蒙·鸿蒙系统
胖咕噜的稞达鸭1 天前
AVL树手撕,超详细图文详解
c语言·开发语言·数据结构·c++·算法·visual studio