using UnityEngine;
using System.Collections;

public class MonoSingleton <T> : MonoBehaviour where T : MonoSingleton <T> {

	#region Static Variables
	private static T _instance;

	public static T Instance {
		get {
			return _instance;
		}
		private set {
			_instance = value;
		}
	}
	#endregion


	#region Events
	void Awake () {

		if (Instance == null) {

			Instance = this as T;
			DontDestroyOnLoad (this);
			
		} else {
			
			DestroyImmediate (this.gameObject);

		}

		AwakeEx ();
		
	}


	void OnDestory () {
		
		OnDestroyEx ();
		if (Instance == this)
			Instance = null;
		
	}
	#endregion


	#region Virtual Methods
	protected virtual void AwakeEx () {}

	protected virtual void OnDestroyEx () {}
	#endregion
}
