多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > 綜合技術(shù) > unity泛型單例

unity泛型單例

來(lái)源:程序員人生   發(fā)布時(shí)間:2015-03-20 09:03:33 閱讀次數(shù):4199次

參考自:http://wiki.unity3d.com/index.php/Singleton

我們要使用Unity3d在Object類(lèi)中提供了1個(gè)靜態(tài)函數(shù) :Object.DontDestroyOnLoad (Object target) . 加載新場(chǎng)景的時(shí)候使單例對(duì)象不被自動(dòng)燒毀

作為 MonoBehaviour 由于我們可能需要協(xié)同程序,所以使用 Lock同步

用法示例

MyClass.cs

public class MyClass : MonoBehaviour { void Awake () { Debug.Log(Manager.Instance.myGlobalVar); } }

Manager.cs

public class Manager : Singleton<Manager> { protected Manager () {} // guarantee this will be always a singleton only - can't use the constructor!   public string myGlobalVar = "whatever"; }

履行

Singleton.cs

using UnityEngine;   /// <summary> /// Be aware this will not prevent a non singleton constructor /// such as `T myT = new T();` /// To prevent that, add `protected T () {}` to your singleton class. /// /// As a note, this is made as MonoBehaviour because we need Coroutines. /// </summary> public class Singleton<T> : MonoBehaviour where T : MonoBehaviour { private static T _instance;   private static object _lock = new object();   public static T Instance { get { if (applicationIsQuitting) { Debug.LogWarning("[Singleton] Instance '"+ typeof(T) + "' already destroyed on application quit." + " Won't create again - returning null."); return null; }   lock(_lock) { if (_instance == null) { _instance = (T) FindObjectOfType(typeof(T));   if ( FindObjectsOfType(typeof(T)).Length > 1 ) { Debug.LogError("[Singleton] Something went really wrong " + " - there should never be more than 1 singleton!" + " Reopening the scene might fix it."); return _instance; }   if (_instance == null) { GameObject singleton = new GameObject(); _instance = singleton.AddComponent<T>(); singleton.name = "(singleton) "+ typeof(T).ToString();   DontDestroyOnLoad(singleton);   Debug.Log("[Singleton] An instance of " + typeof(T) + " is needed in the scene, so '" + singleton + "' was created with DontDestroyOnLoad."); } else { Debug.Log("[Singleton] Using instance already created: " + _instance.gameObject.name); } }   return _instance; } } }   private static bool applicationIsQuitting = false; /// <summary> /// When Unity quits, it destroys objects in a random order. /// In principle, a Singleton is only destroyed when application quits. /// If any script calls Instance after it have been destroyed, /// it will create a buggy ghost object that will stay on the Editor scene /// even after stopping playing the Application. Really bad! /// So, this was made to be sure we're not creating that buggy ghost object. /// </summary> public void OnDestroy () { applicationIsQuitting = true; } }

要求

(從GetOrAddComponent MonoBehaviourExtended.cs)

static public class MethodExtensionForMonoBehaviourTransform { /// <summary> /// Gets or add a component. Usage example: /// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>(); /// </summary> static public T GetOrAddComponent<T> (this Component child) where T: Component { T result = child.GetComponent<T>(); if (result == null) { result = child.gameObject.AddComponent<T>(); } return result; } }


??

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線(xiàn)----------------------------
分享到:
------分隔線(xiàn)----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 久久免费大片 | 国产成人精品一区二区免费 | 一级片大全 | 欧美国产精品亚洲精品第一区 | 婷婷五月在线视频 | 最近高清中文字幕在线国语5 | 亚洲精品久久一区二区无卡 | 最近中文字幕高清字幕 | 亚洲国产日本 | 欧美日韩精品免费一区二区三区 | 国产精品亚洲欧美一级久久精品 | 中文字幕一区二区三区在线播放 | 成人精品视频 | 羞羞免费网页登界面入口 | 国产精品久久久久久久 | 亚洲国产精品一区二区三区在线观看 | 欧美激情综合亚洲五月蜜桃 | 欧美精品亚洲精品 | 欧美日本在线 | 成片免费观看视频在线网 | 精品一区二区免费视频 | 国产欧美日韩免费一区二区 | 国产免费叼嘿在线观看 | 三级全黄在线观看www桃花 | 免费视频爱爱 | 激情文学激情图片 | 国产91高跟丝袜 | 波多野结衣在线网站 | 最新国产福利片在线观看 | 高清国产性色视频在线 | 欧美综合伊人久久 | 国产精品一区二区三区四区五区 | 性高湖久久久久久久久aaaaa | 色老久久精品偷偷鲁一区 | 国产视频h | 亚洲一区二区精品视频 | 久久久高清日本道免费观看 | 久久在线免费观看视频 | 五月花婷婷 | 久久久久久久久久久福利 | 双性h啪啪樱桃动漫直接观看 |