Difference between revisions of "ResourceSingleton"
(Initial page, includes API information and categories) |
(→Setup and Usage: Added usage example) |
||
Line 11: | Line 11: | ||
int currentLevel = 0; | int currentLevel = 0; | ||
List<string> levels = new List<string>(); | List<string> levels = new List<string>(); | ||
+ | string GetCurrentLevel () { ... } | ||
}; | }; | ||
To set up this class for proper usage, create an empty GameObject and attach the script to it. This empty GameObject must be named the same as your script (for the example above, ''GameOptionsManager''). Then drag the object into a folder named [http://docs.unity3d.com/Documentation/ScriptReference/Resources.html Resources] somewhere in your Project view. Once the prefab is created, delete the object from the Hierarchy. Classes that derive from ResourceSingleton should not be placed in the Hierarchy during runtime; only when they are being set up the first time. | To set up this class for proper usage, create an empty GameObject and attach the script to it. This empty GameObject must be named the same as your script (for the example above, ''GameOptionsManager''). Then drag the object into a folder named [http://docs.unity3d.com/Documentation/ScriptReference/Resources.html Resources] somewhere in your Project view. Once the prefab is created, delete the object from the Hierarchy. Classes that derive from ResourceSingleton should not be placed in the Hierarchy during runtime; only when they are being set up the first time. | ||
− | Once the prefab is in your Resources directory, you may modify the public variables on it as you like. When the game is run, a copy of the prefab is instantiated in the scene when you request the '''instance''' of the variable (see below). By only accessing the prefab though the '''instance''' member and not putting it in the Hierarchy, you may load and re-load as many Unity levels as you want, while always having access to this same object. | + | Once the prefab is in your Resources directory, you may modify the public variables on it as you like. When the game is run, a copy of the prefab is instantiated in the scene when you request the '''instance''' of the variable (see below). By only accessing the prefab though the '''instance''' member and not putting it in the Hierarchy, you may load and re-load as many Unity levels as you want, while always having access to this same object. |
+ | |||
+ | To access this class from other scripts, use the public static '''instance''' member as follows: | ||
+ | |||
+ | GameOptionsManager.instance.currentLevel = 3; | ||
+ | Application.LoadLevel (GameOptionsManager.instance.GetCurrentLevel ()); | ||
== Public Variables == | == Public Variables == |
Latest revision as of 18:33, 20 April 2013
This class implements a way create a Singleton object that derives from MonoBehaviour and may be configured using the Inspector window of the Unity Editor.
A Singleton is a kind of object that is globally accessible. There is only one instance of this object allowed to exist at any given time.
This is based upon work from the Unify Community wiki: Generic Based Singleton for MonoBehaviours
Contents |
[edit] Setup and Usage
To use this class, make a new class that derives from it, like this:
public class GameOptionsManager : ResourceSingleton<GameOptionsManager> { int currentLevel = 0; List<string> levels = new List<string>(); string GetCurrentLevel () { ... } };
To set up this class for proper usage, create an empty GameObject and attach the script to it. This empty GameObject must be named the same as your script (for the example above, GameOptionsManager). Then drag the object into a folder named Resources somewhere in your Project view. Once the prefab is created, delete the object from the Hierarchy. Classes that derive from ResourceSingleton should not be placed in the Hierarchy during runtime; only when they are being set up the first time.
Once the prefab is in your Resources directory, you may modify the public variables on it as you like. When the game is run, a copy of the prefab is instantiated in the scene when you request the instance of the variable (see below). By only accessing the prefab though the instance member and not putting it in the Hierarchy, you may load and re-load as many Unity levels as you want, while always having access to this same object.
To access this class from other scripts, use the public static instance member as follows:
GameOptionsManager.instance.currentLevel = 3; Application.LoadLevel (GameOptionsManager.instance.GetCurrentLevel ());
[edit] Public Variables
[edit] instance : T
This will return the only instance of this class allowed to exist. If there is no GameObject containing this object in the scene, a new one is copied out of the Resources folder. This is a static, read-only variable, and is the only way this deriving sub-classes should be accessed.
[edit] Virtual Functions
[edit] void Init ()
This is where deriving sub-classes can initialize their data. It should be used instead of Start ().
[edit] void OnAppQuit ()
This function is what deriving sub-classes should use to do things when the application closes.