注意
私有构造函数不是必须的,因为本来单例就不应该去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()
{
}
}
结果自动创建对象