Last active
August 29, 2015 14:20
-
-
Save suakig/cba20200ede1b4ded428 to your computer and use it in GitHub Desktop.
SingletonMonoBehaviourInResources.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SingletonMonoBehaviourFromResources<T> : MonoBehaviour where T : MonoBehaviour | |
{ | |
private static T instance; | |
public static T Instance { | |
get { | |
if (instance == null) { | |
instance = (T)FindObjectOfType (typeof(T)); | |
if (instance == null) { | |
instance = (ResourcesLoad ()).AddComponent<T> (); | |
instance.name = typeof(T).Name; | |
} | |
} | |
return instance; | |
} | |
} | |
public static GameObject ResourcesLoad() | |
{ | |
return Instantiate (Resources.Load ("Singleton/" + typeof(T).Name)) as GameObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment