Saturday, March 1, 2014

Singleton in Unity3d

Well if you need a singleton, you can use it in Unity3d. 


here is the code:


public class MadalyaMotor : MonoBehaviour {
// Static singleton property
public static MadalyaMotor myInstance { get; private set; }

//public MadalyaMotor()
public void Awake()
{
//-- start of singleton
// First we check if there are any other instances conflicting, better be sure than sorry
if(myInstance != null && myInstance != this)
{
// If that is the case, we destroy other instances
Destroy(gameObject);
}
// Here we save our singleton instance
myInstance = this;
// Furthermore we make sure that we don't destroy between scenes (this is optional)
DontDestroyOnLoad(gameObject);
//-- end of singleton

}
}

Singleton is useful if you want a controller or want o instantiate only one object of a kind. Like audio management , score management, achievement management,.. all  must be done in a static or a singleton object.

Try messing with the code above, put it on an empty unity3d game object, make some functions and call your singleton object from those object. You can make a number of hits singleton controller for example. If you have any questions please do post here.

Well there is also a great blog about this, try http://clearcutgames.net/home/?p=437 if you want further information on singletons. That guy also explains lazy load method of singleton. If you prepfer you can load your gameobjects with lazy load logic so that your singleton object will be created whenever you need them.


Hey i also found a good code example.  Check this out. I also included some helpful page url address so that you can examine them if you want too.

static public bool isActive {
get {
return instance  != null;
}
}

static public MyGameEngine Instance {
get {
//if(instance == null)  {
// instance = Object.FindObjectOfType(typeof(MyGameEngine)) as MyGameEngine;
//}

//some helpful url addresses to check:
//http://answers.unity3d.com/questions/17916/singletons-with-coroutines.html
//http://blog.i-evaluation.com/2013/12/11/creating-game-manager-using-state-machine-and-singleton-pattern-in-unity3d/
//http://forum.unity3d.com/threads/197169-Singleton-vs-Static

if(instance == null)  {
//first time initialize
Debug.Log("instantiate singleton");
GameObject go = new GameObject("_myGameManagerSingleton");
instance = go.AddComponent<MyGameEngine>();
go.transform.position = new Vector3(-20,-20,-20);
//GameObject go  = new GameObject("myGameManagerSingleton", typeof(MyGameEngine));
DontDestroyOnLoad(go);

//Call your other functions to continue inner initialization
instance.DoOtherInitializationStuff();
}
return instance;
}
}

another example (simple yet working great at Unity3d)


private static Singleton instance = null;

 

public static Singleton Instance

{ 

    get { return instance; }   

}

       

private void Awake()

{

    // If instance already exist, destroy ourself.

    if (instance != null && instance != this)

    { 

        Destroy(gameObject);

        return;

    }

 

    // No instance exist yet? We are it.

    instance = this;

 

    // This line exist so that the Singleton would persist between scene loads.

    // Not all singletons needs that.

    DontDestroyOnLoad(gameObject); 

}



No comments:

Post a Comment