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();
    }
}
相关推荐
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机