Unity設(shè)計(jì)模式之單例模式
定義
單例模式(Singleton)是設(shè)計(jì)模式中很常見的一種設(shè)計(jì)模式,目的是為了讓一個(gè)類在程序運(yùn)行期間有且僅有一個(gè)實(shí)例,且方便全局訪問。
實(shí)現(xiàn)
1、私有的構(gòu)造函數(shù)。
2、含有一個(gè)該類的靜態(tài)私有對象。
3、靜態(tài)的公有函數(shù)或?qū)傩裕奖阌脩魟?chuàng)建或者獲取它本身的靜態(tài)私有對象。
適用場景
當(dāng)項(xiàng)目中的某一個(gè)對象,在程序運(yùn)行的過程中,只有一個(gè)對象,可以使用單例模式。
例如:資源管理類,日志管理類等,受項(xiàng)目影響比較小,可以多個(gè)項(xiàng)目共同使用。
優(yōu)缺點(diǎn)
優(yōu)點(diǎn)
1、單例模式只生成一個(gè)實(shí)例,限制對象數(shù)量,減少內(nèi)存開支和系統(tǒng)性能消耗。
2、使用方便,提供全局訪問的函數(shù)或?qū)傩浴?/p>
缺點(diǎn)
1、容易為了使用方便造成過度使用的問題,但并不是要求設(shè)計(jì)者不使用這個(gè)模式,而是在仔細(xì)設(shè)計(jì)和特定的前提下,適當(dāng)使用。
舉例
C# 單例
/// <summary>
/// 一個(gè)簡單的單例
/// </summary>
public class Test
{
#region Instance
private static Test _Instance;
public static Test Instance
{
get
{
if (_Instance == null)
{
_Instance = new Test();
}
return _Instance;
}
}
private Test() { }
#endregion
}
Unity mono單例
相比于C#單例,mono單例需要注意的是
1、創(chuàng)建對象的時(shí)候,需要創(chuàng)建一個(gè)相應(yīng)的對象,然后將腳本添加。
2、對象Awake的時(shí)候,需要判斷是否已經(jīng)創(chuàng)建了對象,如果創(chuàng)建了,需要銷毀當(dāng)前對象。
3、我這里在OnDestroy的時(shí)候,把靜態(tài)變量銷毀了,是因?yàn)橛行┻壿嬂锩婵赡苄枰N毀這個(gè)對象,重新創(chuàng)建。
using UnityEngine;
/// <summary>
/// 一個(gè)簡單的mono單例
/// </summary>
public class Test : MonoBehaviour
{
#region Instance
private static Test _Instance;
public static Test Instance
{
get
{
if (!_Instance)
{
_Instance = GameObject.FindObjectOfType<Test>();
if (!_Instance)
{
GameObject obj = new GameObject();
_Instance = obj.AddComponent<Test>();
}
}
return _Instance;
}
}
private void Awake()
{
if (_Instance)
{
Destroy(gameObject);
}
}
private void OnDestroy()
{
_Instance = null;
}
#endregion
}
通用mono單例
要做一個(gè)通用的mono單例,這里我們需要使用泛型來實(shí)現(xiàn)一個(gè)基類。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// mono單例
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
protected static T _instance;
/// <summary>
/// 是否實(shí)例化
/// </summary>
public static bool initialized;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T>();
if (FindObjectsOfType<T>().Length > 1)
{
return _instance;
}
if (_instance == null)
{
string name = typeof(T).Name;
GameObject obj = GameObject.Find(name);
if (obj == null)
{
obj = new GameObject(name);
}
_instance = obj.AddComponent<T>();
DontDestroyOnLoad(obj);
}
}
initialized = true;
return _instance;
}
}
protected virtual void Awake()
{
if (initialized)
{
Destroy(gameObject);
}
}
protected virtual void OnDestroy()
{
_instance = null;
initialized = false;
}
}
實(shí)現(xiàn)類文章來源:http://www.zghlxwxcb.cn/news/detail-407320.html
using UnityEngine;
/// <summary>
/// 一個(gè)簡單的mono單例
/// </summary>
public class Test : MonoSingleton<Test>
{
public void MonoTest() {
Debug.Log("test");
}
}
尾語
如果有說的不好的地方,歡迎各位大佬批評指正。文章來源地址http://www.zghlxwxcb.cn/news/detail-407320.html
到了這里,關(guān)于Unity設(shè)計(jì)模式之單例模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!