单例模式

注意

私有构造函数不是必须的,因为本来单例就不应该去new一个对象

饿汉式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseManager : MonoBehaviour
{
    void Start()
    {
        GameManager.GetInstance();

    }

}

public class GameManager {


    private static GameManager instance=new GameManager();

    public static  GameManager GetInstance()
    {


        return instance;
    }

}

懒汉式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseManager : MonoBehaviour
{
    void Start()
    {
        GameManager.GetInstance();

    }

}

public class GameManager {


    private static GameManager instance;

    public static  GameManager GetInstance()
    {
        if (instance == null)
            instance = new GameManager();

        return instance;
    }

}

双锁

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseManager : MonoBehaviour
{
    void Start()
    {
        GameManager.GetInstance();

    }

}

public class GameManager {


    private volatile static GameManager instance;
    private readonly static object padLock = new object();
    
    public  static  GameManager GetInstance()
    {
        if (instance == null)
        {
            lock (padLock)
            {
                if (instance == null)
                {
                    instance = new GameManager();
                }
            }
        }
        

        return instance;
    }

}

单例模式基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseManager<T> where T : new()
{

    private static T instance;


    public static T GetInstance()
    {
        if (instance == null)
        {
            instance = new T();
        }
        return instance;
    }

}

public class GameManager : BaseManager<GameManager>
{

}

unity单例继承MonoBehaviour

基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{

    private static T instance;

    public static T GetInstance()
    {
        return instance;
    }


    //子类可以再重写
    protected virtual void Awake()
    {

        instance = this as T;
    }

}

子类(只给一个对象)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : SingletonMono<NewBehaviourScript>
{

    void Start()
    {
        Debug.Log(NewBehaviourScript.GetInstance().name);
    }

    protected override void Awake()
    {
        base.Awake();
    }


}

改良

基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//继承这个基类后就不用手动去把脚本拖到对象上
public class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;

    public static T GetInstance()
    {
        if(instance == null)
        {
            GameObject obj = new GameObject();
            //设置对象的名字为脚本名
            obj.name = typeof(T).ToString();
            //让这个单例对象过场景不移除
            //因为单例对象往往是存在整个程序生命周期的
            DontDestroyOnLoad(obj);
            instance = obj.AddComponent<T>();
        }
        return instance;
    }


  
}

子类(不用拖到对象上)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : SingletonAutoMono<NewBehaviourScript>
{

    public void Test()
    {
        Debug.Log(NewBehaviourScript.GetInstance().name);
    }

 


}

测试一下,这个脚本给摄像机

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        NewBehaviourScript.GetInstance().Test();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

结果自动创建对象
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值