U3D点滴-单例模式
来源:互联网 发布:做淘宝月收入多少 编辑:程序博客网 时间:2024/05/25 20:00
例如:对于一个音乐管理类
using
UnityEngine;
public
class
MusicManager : MonoBehaviour
{
public
void
Play()
{
//Play some audio!
}
}
现在要调用来播放音乐,如下代码
Object.FindObjectOfType < MusicManager > ().Play();
以上的代码不仅难懂而且不安全,比较的好方式如下代码:
var musicManager = Object.FindObjectOfType < MusicManager >();
if ( musicManager != null )
{
musicManager.Play ();
}
以上的代码运行没有问题,但是每次调用的话会比较麻烦。
现在就用单例模式来创建代码:
public
class
MusicManager : MonoBehaviour
{
//We make a static variable to our MusicManager instance
public
static
MusicManager instance {
get
;
private
set
; }
//When the object awakens, we assign the static variable
void
Awake()
{
instance =
this
;
}
public
void
Play()
{
//Play some audio!
}
}
//...
//Now in another class, we can call Play() by using the static variable!
public
class
LevelController : MonoBehaviour
{
void
PlayMusic()
{
MusicManager.instance.Play();
}
}
这样的方式叫做lazy Singleton(偷懒的单例模式吗?!)
这样使用要注意一个地方就是调用的时间,就是如果要使用不能在awake()方法里面,以保证这个实例不为null
比如,Start()方法就可以。
以下是标准的单例模式
public
class
MusicManager : MonoBehaviour
{
//Here is a private reference only this class can access
private
static
MusicManager _instance;
//This is the public reference that other classes will use
public
static
MusicManager instance
{
get
{
//If _instance hasn't been set yet, we grab it from the scene!
//This will only happen the first time this reference is used.
if
(_instance ==
null
)
_instance = GameObject.FindObjectOfType<MusicManager>();
return
_instance;
}
}
public
void
Play()
{
//Play some audio!
}
}
持续的单例
有时候你可能要在不同的场景里面使用单例
public
class
MusicManager : MonoBehaviour
{
private
static
MusicManager _instance;
public
static
MusicManager instance
{
get
{
if
(_instance ==
null
)
{
_instance = GameObject.FindObjectOfType<MusicManager>();
//Tell unity not to destroy this object when loading a new scene!
DontDestroyOnLoad(_instance.gameObject);
}
return
_instance;
}
}
void
Awake()
{
if
(_instance ==
null
)
{
//If I am the first instance, make me the Singleton
_instance =
this
;
DontDestroyOnLoad(
this
);
}
else
{
//If a Singleton already exists and you find
//another reference in scene, destroy it!
if
(
this
!= _instance)
Destroy(
this
.gameObject);
}
}
public
void
Play()
{
//Play some audio!
}
}
DontDestroyOnLoad() will maintain all scripts that are placed on the same GameObject as your singleton.
For this reason it’s usually a good idea to put a singleton on it’s own GameObject alone.
Other Notes:
- DontDestroyOnLoad() only needs to be used on objects inheriting MonoBehaviour. A static reference in a normal class will maintain its data across scenes.
- In Unity, if you have a reference to a GameObject or MonoBehaviour that has been destroyed, it will equal null. The singleton examples above use this to their advantage.
原文引用:http://unitypatterns.com/singletons/
0 0
- U3D点滴-单例模式
- 【U3D设计模式】单例模式
- U3D点滴-Coroutines
- U3D点滴-计时器
- U3D开发点滴-摄像机跟随
- U3D开发点滴-延迟物体消失
- U3D开发点滴-敌人基础类
- U3D开发点滴-射线的使用
- iOS-单例模式
- 设计模式-单例
- 剑指-单例模式
- 设计模式--单例模式
- 设计模式--单例模式
- Java设计模式-单例模式
- 设计模式-单例模式
- PHP设计模式-单例模式
- PHP设计模式-单例模式
- 设计模式-单例模式
- Thread 1 cannot allocate new log
- 关于android百度地图离线地图的使用
- 网站发展移动端选择App or HTML5?
- 获取当前app的名称和版本号
- -Z(IDATA)ISTACK+_IDATA_STACK_SIZE#08-_IDATA_END",
- U3D点滴-单例模式
- 淘金网UCskype呼叫中心(语音群呼)系统
- Android WebView js与Java调用实例
- getElementById用法
- 六种常见的数据加载模式设计
- QMap 和 QHash
- C基础,计算机中的数以及volatile限定修饰符,指针及其注意事项,external,static,Void *
- Android init进程启动分析
- Android安全退出应用程序