Skip to content

Instantly share code, notes, and snippets.

@suakig
Last active August 29, 2015 14:20
Show Gist options
  • Save suakig/cba20200ede1b4ded428 to your computer and use it in GitHub Desktop.
Save suakig/cba20200ede1b4ded428 to your computer and use it in GitHub Desktop.
SingletonMonoBehaviourInResources.cs
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