这文章展示了 怎样编写一个类 它能在场景中可以由脚本随时调用,而不需要手动指定对象。
这个非常有用例如游戏中需要随时访问的:得分情况,生命等等
提示:
比较好的做法是创建一个空物体,命名为Managers 并且连接所有的管理动作在它上面(如AManager ,BManager,CManager...)。
用法:
使用下面的模板代将创建一个AManager类,记住:它将取代场景中所有以AManager命名的类
用法示例:
比如访问AManager中的Foo()函数,AManager.instance.Foo();
C# - AManager.cs
using UnityEngine;
usingSystem.Collections;
///
/// AManager 是唯一的
///他的静态方法,避免了手动的去连接到每一个需要它的地方,所以需要他时只需写下 :
///AManager.instance.DoSomeThing();
///
public class AManager : MonoBehaviour {
// s_Instance参数 通常是隐藏的,我们不需要看见它
private static AManager s_Instance = null;
// 这里定义了一个静态的实例化属性,它将查找在场景中的manager object并且返回它
//插一句:public static AManager instance C#创建静态的构造函数的方法,优于构造函数执行,它只执行一次,确保不会被实例化n次
public static AManager instance {
get {
if (s_Instance == null) {
// FindObjectOfType(...) 返回场景中的第一个 AManager object
s_Instance = FindObjectOfType(typeof (AManager)) as AManager;
if (s_Instance == null)
Debug.Log ("Could not locate an AManager object. \
You have to have exactly one AManager in the scene.");
}
return s_Instance;
}
}
// 确保实例化的对象被销毁
void OnApplicationQuit() {
s_Instance = null;
}
// 这里添加自己的代码...
public void DoSomeThing() {
Debug.Log("Doing something now", this);
}
}
导读
基础示例:
using UnityEngine;
public class MySingleton : MonoBehaviour
{
private static MySingleton instance;
public static MySingleton Instance
{
get
{
if (instance == null)
{
instance = new GameObject ("MySingleton").AddComponent<MySingleton> ();
}
return instance;
}
}
public void OnApplicationQuit ()
{
instance = null;
}
}
示例,游戏得分处理
The singleton in this example keeps track of the game score. Getting and setting this value is done like so:
这个示例将展示如何处理游戏中的得分
MySingleton.Instance.Score += 5;
Debug.Log ("Score is now: " + MySingleton.Instance.Score);
类如下:
public class MySingleton
{
private static MySingleton instance;
public MySingleton ()
{
if (instance != null)
{
Debug.LogError ("Cannot have two instances of singleton. Self destruction in 3...");
return;
}
instance = this;
}
public static MySingleton Instance
{
get
{
if (instance == null)
{
new MySingleton ();
}
return instance;
}
}
private int score;
public int Score
{
get
{
return score;
}
set
{
score = value;
}
}
}
给物体广播一跳消息
MySingleton.Instance.Register (gameObject);
MySingleton.Instance.Unregister (gameObject);
using System.Collections;
using UnityEngine;
public class MySingleton
{
private static MySingleton instance;
public MySingleton ()
{
if (instance != null)
{
Debug.LogError ("Cannot have two instances of singleton. Self destruction in 3...");
return;
}
instance = this;
Init ();
}
public static MySingleton Instance
{
get
{
if (instance == null)
{
new MySingleton ();
}
return instance;
}
}
private int timer;
private ArrayList listeners;
private void Init ()
{
listeners = new ArrayList ();
}
public int Timer
{
get
{
return timer;
}
set
{
timer = value;
if (timer <= 0)
{
foreach (GameObject listener in listeners)
{
listener.SendMessage ("GameOver");
}
}
}
}
public GameObject RegisterListener (GameObject listener)
{
listeners.Add (listener);
return listener;
}
public bool UnregisterListener (GameObject listener)
{
if (!listeners.Contains (listener))
{
return false;
}
listeners.Remove (listener);
}
}