這文章展示了 怎樣編寫一個類 它能在場景中可以由腳本隨時調(diào)用,而不需要手動指定對象。
這個非常有用例如游戲中需要隨時訪問的:得分情況,生命等等
提示:
比較好的做法是創(chuàng)建一個空物體,命名為Managers 并且連接所有的管理動作在它上面(如AManager ,BManager,CManager...)。
用法:
使用下面的模板代將創(chuàng)建一個AManager類,記住:它將取代場景中所有以AManager命名的類
用法示例:
比如訪問AManager中的Foo()函數(shù),AManager.instance.Foo();
C# - AManager.cs
using UnityEngine;
usingSystem.Collections;
///
/// AManager 是唯一的
///他的靜態(tài)方法,避免了手動的去連接到每一個需要它的地方,所以需要他時只需寫下 :
///AManager.instance.DoSomeThing();
///
public class AManager : MonoBehaviour {
// s_Instance參數(shù) 通常是隱藏的,我們不需要看見它
private static AManager s_Instance = null;
// 這里定義了一個靜態(tài)的實例化屬性,它將查找在場景中的manager object并且返回它
//插一句:public static AManager instance C#創(chuàng)建靜態(tài)的構(gòu)造函數(shù)的方法,優(yōu)于構(gòu)造函數(shù)執(zhí)行,它只執(zhí)行一次,確保不會被實例化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);
}
}
導(dǎo)讀
基礎(chǔ)示例:
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);
}
}