Unity3D单例类模板类的实现
By D.S.Qiu
尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com
无意中Unity工程中看到Singleton的类,因为之前D.S.Qiu在项目中也用了Singleton来做所有View的单例类模板,有种“英雄所见略同”的赶脚(虽然此处用法不当),所以贴出来分享下:
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!" + " Reopenning 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; } }
小结:
别人实现的就是考虑到位,同步锁和程序退出的情况,D.S.Qiu实现的版本都没有做这样的考虑。不过,D.S,Qiu的View其实是用来管理UI的但没有继承MonoBehavior,只是一个普通的C#类,便于自己管理,不过这样有一个劣势就是不能使用MonoBehavior的行为,如协程等特性。
如果您对D.S.Qiu有任何建议或意见可以在文章后面评论,或者发邮件(gd.s.qiu@gmail.com)交流,您的鼓励和支持是我前进的动力,希望能有更多更好的分享。
转载请在文首注明出处:http://dsqiu.iteye.com/blog/2017655
更多精彩请关注D.S.Qiu的博客和微博(ID:静水逐风)
相关推荐
Unity单例模式模板类,以供学习,有两个: 一个单纯的Singleton 一个MonoSingleton,继承于MonoBehaviour
unity3d 美食游戏模板unity3d 美食游戏模板unity3d 美食游戏模板unity3d 美食游戏模板
Unity3d源码 街机太空射击战斗游戏开发模板 Space Combat Kit v2.0 Unity商店下载的精品游戏源码 , 适合学习及二次开发使用 , 是个完整的Unitypackage包 , 直接导入到空项目就可以直接查看效果了 Unity3d源码 街机...
Unity游戏源码,简单的demo , 适合新手学习和二次开发 。 是unitypackage包 , C#语言的 C#语言的 C#语言的 重要的事说三遍! 新建个空项目直接导入就行了,无需其他操作 ,...Unity3D刺激火力全开丛林跑酷游戏模板代码
单例模板类,实现vector中结构体数据的排序。
Unity3D源码 Action-RPG Starter Kit 6.01 动作游戏模板
unity3d 狙击游戏开发模板unity3d 狙击游戏开发模板unity3d 狙击游戏开发模板unity3d 狙击游戏开发模板unity3d 狙击游戏开发模板
主要介绍了C++单例类模板,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
单例模式是最简单的设计模式之一。在实际工程中,如果一个类的对象重复持有资源的成本很高,且对外接口... 二是实现一个类模板,其模板参数是希望由单例管理的类的名字,并提供 GetInstance 之类的静态接口。这种做法的
unity3d恐怖游戏开发模板unity3d恐怖游戏开发模板unity3d恐怖游戏开发模板unity3d恐怖游戏开发模板
unity3d游戏课程设计报告.pdf
1、什么是单例模式 在架构设计时,某些类在整个系统生命周期中最多只能有一...2、怎么实现单例模式 思考:如何定义一个类,使得这个类最多只能创建一个对象? 分析:因为对象是由构造函数创建的,所以我们应该
使用qt5开发的单例模板,可存取正常的类以及指针,一般用于大型的软件架构。
此工具实现快捷编辑Unity3D的脚本模板,使用预定义标记让Unity3D在脚本创建时自动添加文件头注释,更新版权时间申明和脚本创建日期等信息。
Unity通用WebGL模板Universal WebGL Template 1.2.1 仅供学习,请勿商用。
一个使用Unity3D实现的2D无尽跑酷游戏。
2D角色定制Unity3d 源码模板(Unity3d) unity2019以上版本 介绍: 我使用的超级英雄学院角色定制/创建工具 我们现在已经完成了角色定制器的任务,但您最有可能想要做的下一步是在游戏中使用角色并让他/她执行一些令...