单例模式是设计模式中很常用的一种模式,它的目的是让一个类在程序运行期间有且只有一个实例。
一 、单例模式优点
(1)单例模式核心在于对于某个单例类,在系统中同时只存在唯一一个实例,并且该实例容易被外界所访问;
(2)意味着在内存中,只存在一个实例,减少了内存开销;
二 、单例模式特点
(1)只存在唯一一个实例;
(2)提供统一对外访问接口,使得全局可对该单例的唯一实例进行访问;
(3)自行实例化(私有构造函数,不允许外界对其进行实例化)。
三 、单例模式注意点
(1)注意线程安全问题,在多线程、高并发的情况下,可能同时产生多个实例,违背了单例模式。
(2)Unity中如果过度使用单例模式,将会导致代码耦合度非常高,脚本与脚本之间的耦合,代码的后续拓展变得非常麻烦。
(3)Unity中暂时不需要考虑多线程问题,Unity就只有一个主线程和开启多个辅助协程,不会出现多线程并发问题。
(4)控制游戏对象的生成和销毁并不建议使用单例模式,可通过主游戏逻辑InGame进行事件下发,自行管理Update,使用工厂来进行对象的创建和销毁。
四 、常见单例模式
1、单例一:最简单的单例方法—继承MonoBehaviour
(1)创建一个名叫:Manager.cs的脚本,实现方法如下:
/****************************************************
文件:Manager.cs
作者:xxx
邮箱:
日期:2021/04/19 10:28:38
功能:
*****************************************************/
using UnityEngine;
public class Manager : MonoBehaviour
{
public static Manager Instance;
void Awake()
{
Instance=this;
}
public void TestMethod()
{
print("最简单的单例方法");
}
}
(2)如何调用?新建一个Main.cs的脚本来调用它。
/****************************************************
文件:Main.cs
作者:xxx
邮箱:
日期:2021/04/19 10:29:27
功能:
*****************************************************/
using UnityEngine;
public class Main : MonoBehaviour
{
void Start()
{
Manager.Instance.TestMethod();
}
}
2、单例二:“规范”一点的单例—继承MonoBehaviour
(1)
/****************************************************
文件:Manager.cs
作者:xxx
邮箱:
日期:2021/04/19 10:28:38
功能:
*****************************************************/
using UnityEngine;
public class Manager : MonoBehaviour
{
public static Manager _instance;
public static Manager Instance
{
get
{
if(null == _instance)
{
_instance = FindObjectOfType(typeof(Manager)) as Manager;
}
return _instance;
}
}
public void TestMethod()
{
print("规范一点的单例方法");
}
}
3、单例方法—不继承MonoBehaviour
/****************************************************
文件:Manager.cs
作者:xxx
邮箱:
日期:2021/04/19 10:28:38
功能:
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager {
#region 最简单的单例方法
//public static Manager Instance;
//void Awake()
//{
// Instance = this;
//}
//public void TestMethod()
//{
// print("最简单的单例方法");
//}
#endregion
#region 规范一点的单例方法
//public static Manager _instance;
//public static Manager Instance
//{
// get
// {
// if (null == _instance)
// {
// _instance = FindObjectOfType(typeof(Manager)) as Manager;
// }
// return _instance;
// }
//}
//public void TestMethod()
//{
// print("规范一点的单例方法");
//}
#endregion
#region 单例方法---不继承mono
public static Manager _instance;
public static Manager Instance
{
get
{
if (null == _instance)
{
_instance = new Manager();
}
return _instance;
}
}
public void TestMethod()
{
Debug.Log("不继承Mono单例方法");
}
#endregion
}
注意:因不继承MonoBehaviour,所以不需要将Manager.cs脚本挂载到GameObject组件上。
4、最规范的单例方法—继承MonoBehaviour(不随着场景切换而销毁)
(1)首先,写一个单例的基类
/****************************************************
文件:NewBehaviourScript.cs
作者:xxx
邮箱:
日期:2021/04/19 11:08:30
功能:
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T :Singleton<T>
{
protected static T _instance = null;
public static T Instance
{
get
{
if (null == _instance)
{
//寻找不销毁的DD组件
GameObject go = GameObject.Find("DD");
if (null == go)
{
go = new GameObject("DD");//创建DD
DontDestroyOnLoad(go); //不销毁
}
//在DD上得到T
_instance = go.GetComponent<T>();
//如果_instance为空,说明DD组件没有T组件
if (null == _instance)
{
_instance = go.AddComponent<T>(); //给DD组件添加T组件。并且赋值给_instance
}
}
return _instance;
}
}
}
(2)修改Manager.cs脚本,如下:
/****************************************************
文件:Manager.cs
作者:xxx
邮箱:
日期:2021/04/19 10:28:38
功能:
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : Singleton<Manager>
{
public void TestMethod()
{
Debug.Log("规范单例方法");
}
}
5、最规范的单例方法—不继承MonoBehaviour(不随着场景切换而销毁)
(1)首先写一个不继承Mono的基类
/****************************************************
文件:NewBehaviourScript1.cs
作者:xxx
邮箱:
日期:2021/04/19 11:28:22
功能:
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Singleton<T> where T: class ,new()
{
protected static T _instance = null;
public static T Instance
{
get
{
if (null == _instance)
{
_instance = new T();
}
return _instance;
}
}
protected Singleton()
{
if (null != _instance)
{
Debug.Log("_instance不是null !!!");
Init();
}
}
public virtual void Init()
{
}
}
(2)修改Manager.cs脚本
public class Manager : Singleton<Manager>
{
public override void Init()
{
base.Init();
Debug.Log("初始化Manager");
}
public void TestMethod()
{
Debug.Log("规范不继承单例方法");
}
}
(3)修改Main.cs脚本,代码如下:
/****************************************************
文件:Main.cs
作者:xxx
邮箱:
日期:2021/04/19 10:29:27
功能:
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour {
// Use this for initialization
void Start ()
{
Manager.Instance.TestMethod();
Manager.Instance.Init();
}
}