游戏设计模式-单例

普通类单例几种制作方式如下:

/*
 * Author:W
 * 单例模式
 * 【注意】会随着场景的切换而销毁
 */
public class Singleton_Common
{
    /// <summary>
    /// 普通的单例模式
    /// </summary>
    private static Singleton_Common _instance;
    public static Singleton_Common Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Singleton_Common();
            }

            return _instance;
        }
    }

    /// <summary>
    /// 构造方法私有化
    /// </summary>
    private Singleton_Common() { }
}

/// <summary>
/// 普通单例的基类即接口
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class Single_Template<T> where T: class, new()
{
    protected static T _instance;
    
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new T();
            }

            return _instance;
        }
    }

    protected Single_Template()
    {
        if (_instance != null)
        {
            Debug.LogError("This "+(typeof(T)).ToString()+" Singleton Instance is not null");
        }

        Init();
    }

    public virtual void Init()
    {
        
    }
}

/// <summary>
/// 在多线程中使用中保证一个单例不会被多次new的情况
/// </summary>
public class Singleton_MultiThread
{
    private static volatile Singleton_MultiThread _instance;

    private static object locker = new object();
    public static Singleton_MultiThread Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (locker)
                {
                    if (_instance == null)
                    {
                        _instance = new Singleton_MultiThread();
                    }
                }
            }

            return _instance;
        }
    }
}

继承于MonoBehaviour类的单例几种制作方式如下:

/// <summary>
/// 继承于MonoBehaviour,最简单单例
/// 【注意】:会随着场景的切换而销毁
/// </summary>
public class Singleton_Mono_Simple : MonoBehaviour
{
    public static Singleton_Mono_Simple Instance;

    void Awake()
    {
        Instance = this;
    }
}

/// <summary>
///  继承于MonoBehaviour,单例的规范写法
///  【注意】:会随着场景的切换而销毁
/// </summary>
public class Singleton_Mono_Common : MonoBehaviour
{
    private static Singleton_Mono_Common _instance;
    public static Singleton_Mono_Common Instance
    {
        get
        {
            if (_instance == null)
                _instance = FindObjectOfType(typeof(Singleton_Mono_Common)) as Singleton_Mono_Common;

            return _instance;
        }
    }

    void Awake()
    {
        _instance = this;
    }
}

/// <summary>
/// 继承于MonoBehaviour,单例模式的基类即接口
/// 【注意】不会随着场景的切换而销毁
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class Singleton_Mono_template<T> : MonoBehaviour where T:Singleton_Mono_template<T>
{    
    protected static T _instance = null;
    public static T Instance
    {
        get 
        {
            if (_instance == null)
            {
                string name = typeof(T).Name;

                GameObject singleObj = GameObject.Find(name);
                if (singleObj == null)
                {
                    singleObj = new GameObject(name);
                    DontDestroyOnLoad(singleObj);
                }

                _instance = singleObj.GetComponent<T>();

                if (_instance == null)
                    _instance = singleObj.AddComponent<T>();
            }

            return _instance;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值