日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > unity泛型單例

unity泛型單例

來源:程序員人生   發布時間:2015-03-20 09:03:33 閱讀次數:4201次

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

我們要使用Unity3d在Object類中提供了1個靜態函數 :Object.DontDestroyOnLoad (Object target) . 加載新場景的時候使單例對象不被自動燒毀

作為 MonoBehaviour 由于我們可能需要協同程序,所以使用 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; } }


??

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 日本综合在线 | 看中国黄色毛片 | 91红桃视频 | 天天操天天干天天 | 欧美日韩精品电影 | 午夜一级毛片 | 综合av第一页| 国产精品美女久久久久 | 毛片视频网站 | 国产精品视频一区二区三区四 | 99精品视频免费 | www激情com | 婷婷涩五月 | 国产精品免费一区二区三区都可以 | 国产精品久久久99 | 成人国产精品 | 国产一区二区日韩 | 99re66热这里只有精品8 | 韩日三级电影 | 国产精品又黄又爽又色 | 欧美福利 | 国产午夜亚洲精品理论片色戒 | 日韩激情在线 | 国产精品久久久久久久久久三级 | 欧美一区二区三区在线看 | tv电影天堂 | 中文在线资源天堂 | 国产91在线 | 亚洲 | www.男人天堂 | 久久久蜜桃一区二区 | 成人毛片视频在线观看 | 91久久精品国产91久久 | 宅男天堂 | 亚洲黄色在线视频 | 97av精品| 久久精品一区二区三区不卡牛牛 | 国产一区二区毛片 | 国产在线播放91 | 一级二级在线观看 | 91麻豆精品国产91久久久资源速度 | 久久精品免费观看 |