Singleton
(→Score tracking and GameOver broadcasting example) |
|||
Line 125: | Line 125: | ||
} | } | ||
}</csharp> | }</csharp> | ||
− | == Score tracking and GameOver broadcasting | + | == Score tracking and GameOver broadcasting singleton == |
This singleton expands on the score tracking singleton by also maintaining a list of GameObjects which register and unregister themselves with the singleton in order to receive a "GameOver" message when the score reaches zero or lower. The score is set just like in the singleton above and GameObjects register and unregister with the singleton like so: | This singleton expands on the score tracking singleton by also maintaining a list of GameObjects which register and unregister themselves with the singleton in order to receive a "GameOver" message when the score reaches zero or lower. The score is set just like in the singleton above and GameObjects register and unregister with the singleton like so: | ||
<csharp>MySingleton.Instance.Register( gameObject ); | <csharp>MySingleton.Instance.Register( gameObject ); |
Revision as of 13:13, 28 January 2009
By AngryAnt.
Contents |
Description
People have recently asked about singleton creation quite often in the IRC channel so rather than retyping a basic implementation each time, I'll have this to link to. Yay! :)
Curious, but not quite sure what a singleton is? Ask a friend: [1]
Singletons are generally handy for providing easy access to game state and control code. I've provided example implementation for a basic class type which needn't be attached to a game object in order to function and after this an implementation which works as any other component.
Members of both singleton types are accessed the same way:
<csharp>MySingleton.Instance.MySingletonMember;</csharp>
Note, however that for the component-based singleton (the second implementation), you shouldn't attempt to access the instance earlier than in Start of the calling component.
The non-component example
<csharp>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; } } }</csharp>
Component-based example
<csharp>using UnityEngine;
public class MySingleton : MonoBehaviour { private static MySingleton instance;
public void Awake() { if( instance != null ) { Debug.LogError( "Cannot have two instances of singleton. Self destruction in 3..." ); Destroy( this ); return; }
instance = this; }
public static MySingleton Instance { get { return instance; } }
public void OnApplicationQuit() { instance = null; } }</csharp>
Score tracking singleton
The singleton in this example keeps track of the game score. Getting and setting this value is done like so: <csharp>MySingleton.Instance.Score = 5;</csharp> <csharp>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 Score { get { return score; } set { score = value; } } }</csharp>
Score tracking and GameOver broadcasting singleton
This singleton expands on the score tracking singleton by also maintaining a list of GameObjects which register and unregister themselves with the singleton in order to receive a "GameOver" message when the score reaches zero or lower. The score is set just like in the singleton above and GameObjects register and unregister with the singleton like so: <csharp>MySingleton.Instance.Register( gameObject ); MySingleton.Instance.Unregister( gameObject );</csharp> <csharp>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; } public static MySingleton Instance { get { if( instance == null ) { new MySingleton();
instance.Init();
} return instance; } }
private int score; private ArrayList listener;
private void Init() { listener = new ArrayList(); }
public Score { get { return score; } set { score = value; if( score <= 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 ); } }</csharp>