AManagerClass

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Shows how to automatically locate an instance of a singleton object)
Line 12: Line 12:
 
  /// To avoid having to manually link an instance to every class that needs it, it has a static property called
 
  /// To avoid having to manually link an instance to every class that needs it, it has a static property called
 
  /// instance, so other objects that need to access it can just call:
 
  /// instance, so other objects that need to access it can just call:
  /// AManager.instance.DoSomeThing();
+
  ///       AManager.instance.DoSomeThing();
 
  ///
 
  ///
 
  public class AManager : MonoBehaviour {
 
  public class AManager : MonoBehaviour {
// s_Instance is used to cache the instance found in the scene so we don't have to look it up every time.
+
    // s_Instance is used to cache the instance found in the scene so we don't have to look it up every time.
private static AManager s_Instance = null;
+
    private static AManager s_Instance = null;
 
   
 
   
// This defines a static instance property that attempts to find the manager object in the scene and
+
    // This defines a static instance property that attempts to find the manager object in the scene and
// returns it to the caller.
+
    // returns it to the caller.
public static AManager instance {
+
    public static AManager instance {
get {
+
        get {
if (s_Instance == null) {
+
            if (s_Instance == null) {
// This is where the magic happens. FindObjectOfType(...) returns the first AManager object in the scene.
+
                // This is where the magic happens. FindObjectOfType(...) returns the first AManager object in the scene.
s_Instance =  FindObjectOfType(typeof (AManager)) as AManager;
+
                s_Instance =  FindObjectOfType(typeof (AManager)) as AManager;
if (s_Instance == null)
+
                if (s_Instance == null)
Debug.Log ("Could not locate a AManager object. You have to have exactly one FactionCardMgr in the scene.");
+
                    Debug.Log ("Could not locate a AManager object. You have to have exactly one AManager in the scene.");
}
+
            }
+
           
return s_Instance;
+
            return s_Instance;
}
+
        }
}
+
    }
+
   
// Add the rest of the code here...
+
    // Add the rest of the code here...
public void DoSomeThing() {
+
    public void DoSomeThing() {
Debug.Log("Doing something now", this);
+
        Debug.Log("Doing something now", this);
}
+
    }
 
    
 
    
 
  }
 
  }

Revision as of 04:38, 1 December 2005

Author Keli Hlodversson (freyr)


The following snippet shows how to add a static property called instance that will automatically find an instance of the class in the scene and return it.

This is useful for managers and other behaviours that only have one instance in the scene and need to be accessed from other classes, as it avoids having each class to keep a reference to the manager object.

using UnityEngine;
using System.Collections; 

/// AManager is a singleton.
/// To avoid having to manually link an instance to every class that needs it, it has a static property called
/// instance, so other objects that need to access it can just call:
///        AManager.instance.DoSomeThing();
///
public class AManager : MonoBehaviour {
   // s_Instance is used to cache the instance found in the scene so we don't have to look it up every time.
   private static AManager s_Instance = null;

   // This defines a static instance property that attempts to find the manager object in the scene and
   // returns it to the caller.
   public static AManager instance {
       get {
           if (s_Instance == null) {
               // This is where the magic happens. FindObjectOfType(...) returns the first AManager object in the scene.
               s_Instance =  FindObjectOfType(typeof (AManager)) as AManager;
               if (s_Instance == null)
                   Debug.Log ("Could not locate a AManager object. You have to have exactly one AManager in the scene.");
           }
           
           return s_Instance;
       }
   }
   
   // Add the rest of the code here...
   public void DoSomeThing() {
       Debug.Log("Doing something now", this);
   }
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox