unity中通用单例模板

 Singleton<T>

using System;
using System.Reflection;
namespace LJFramework
{
    public abstract class Singleton<T> where T : Singleton<T>
    {
        private static T mInstance;
        public static T Instance
        {
            get
            {
                if (mInstance == null)
                {
                    var ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
                    var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
                    if (ctor == null)
                    {
                        throw new Exception("Non-public ctor() not found!");
                    }
                    mInstance = ctor.Invoke(null) as T;
                }
                return mInstance;
            }
        }
        protected Singleton()
        {

        }
    }
}

SingletonExample 

using UnityEngine;
namespace LJFramework
{
    public class SingletonExample : Singleton<SingletonExample>
    {
        private SingletonExample()
        {
            Debug.Log("singtleton example ctor");
        }
#if UNITY_EDITOR
        [UnityEditor.MenuItem("LJFramework/Examples/5.SingletonExample", false, 5)]
        static void MenuClicked()
        {
            var initInstance = SingletonExample.Instance;
            initInstance = SingletonExample.Instance;
        }
#endif
    }

}

MonoSingleton<T>:MonoBehaviour

using UnityEngine;

namespace LJFramework
{
    public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        protected static T mInstance = null;
        public static T Instance
        {
            get
            {
                if (mInstance == null)
                {
                    mInstance = FindObjectOfType<T>();
                    if (FindObjectsOfType<T>().Length > 1)
                    {
                        Debug.LogWarning("More than 1");
                        return mInstance;
                    }
                    if (mInstance == null)
                    {
                        var instanceName = typeof(T).Name;
                        Debug.LogFormat("Instance Name: {0}", instanceName);
                        var instanceObj = GameObject.Find(instanceName);
                        if (!instanceObj)
                            instanceObj = new GameObject(instanceName);
                        mInstance = instanceObj.AddComponent<T>();
                        DontDestroyOnLoad(instanceObj); //保证实例例不不会被释放

                        Debug.LogFormat("Add New Singleton {0} in Game!",
                        instanceName);
                    }
                    else
                    {
                        Debug.LogFormat("Already exist: {0}", mInstance.name);
                    }
                }
                return mInstance;
            }
        }
        protected virtual void OnDestroy()
        {
            mInstance = null;
        }
    }
}

MonoSingletonExample 

using UnityEngine;
namespace LJFramework
{
    public class MonoSingletonExample : MonoSingleton<MonoSingletonExample>
    {

#if UNITY_EDITOR
        [UnityEditor.MenuItem("LJFramework/Examples/6.MonoSingletonExample",
        false, 6)]
        private static void MenuClicked()
        {
            UnityEditor.EditorApplication.isPlaying = true;

        }
#endif
        [RuntimeInitializeOnLoadMethod]
        private static void Example()
        {
            var initInstance = MonoSingletonExample.Instance;
            initInstance = MonoSingletonExample.Instance;
        }
    }
}

 

### 实现和使用事件 在 Unity 中,C# 事件可以用于创建响应式的交互逻辑。通过结合设计模式,能够构建更加模块化和可维护的代码结构[^1]。 对于事件的应用,在大型项目中推荐采用发布/订阅模式来解耦不同部分之间的依赖关系。这可以通过定义全局事件中心或者利用现有的消息传递框架完成。当某个特定条件满足时触发相应处理函数执行一系列操作: ```csharp using UnityEngine; public class EventManager : MonoBehaviour { public delegate void OnGameStartHandler(); public static event OnGameStartHandler OnGameStart; private void Start() { // 订阅者注册到该事件上 if (OnGameStart != null) OnGameStart(); } } ``` 上述子展示了如何声明一个简的游戏启动事件处理器,并允许其他脚本监听此事件以便于做出反应。 另外还可以基于Unity原生组件如Button按钮控件自带onClick属性作为UI交互入口点设置回调方法调用;或是开发一套完整的自定义事件分发体系支持更复杂的业务需求场景下的灵活配置与管理。 ### 模式的最佳实践 针对模式而言,根据是否继承 `MonoBehaviour` 类型分为两种情况讨论其具体实现方式[^2]。 #### 继承 MonoBehavior 的 如果希望保持对生命周期的支持以及方便挂载至场景节点,则应考虑如下做法: ```csharp using UnityEngine; public sealed class Singleton<T> : MonoBehaviour where T : Component { private static T _instance; public static T Instance { get; private set; } protected virtual void Awake(){ if (_instance == null){ _instance = this as T; Instance = _instance; DontDestroyOnLoad(gameObject); }else{ Destroy(gameObject); } } } // 使用示:MySingleton.cs public class MySingleton : Singleton<MySingleton>{} ``` 这段代码片段提供了一个通用模板供任何想要成为的游戏对象所继承。它确保了即使加载新场景也不会销毁当前存在的唯一实副本。 #### 不继承 MonoBehavior 的纯 C# 而对于不需要附加到GameObject上的服务层或工具类来说,可以直接按照传统意义上的静态成员变量加私有构造器的方式来进行编码: ```csharp public sealed class Test { private static readonly Lazy<Test> lazy = new Lazy<Test>(() => new Test()); public static Test SingTonle => lazy.Value; private Test(){} } ``` 这里采用了延迟初始化特性(`Lazy`)使得资源分配尽可能晚发生从而提高性能效率的同时也简化了线程安全性的考量[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值