using UnityEngine;
using System.Collections;
using System;

public class SingletonBehaviour<T> : MonoBehaviour where T : MonoBehaviour
	
{
	protected static T _instance;
	
	//public static Func<bool, T> Instance = Creator;

	public static T Instance {get {return Creator(true);}}
	
	private static T Creator(bool persistent)
	{
		if (_instance == null)
		{
			_instance = (T)UnityEngine.Object.FindObjectOfType(typeof(T));
			
			if (!_instance)
			{
				GameObject container = new GameObject();
				container.name = "__" + typeof(T).ToString();
				_instance = container.AddComponent<T>();
				if (persistent){
					UnityEngine.Object.DontDestroyOnLoad(_instance.gameObject);
				}
				else
				{
					UnityEngine.Object.Destroy(_instance);
				}
			}
		}
		return _instance;
	}
}