Skip to content

Instantly share code, notes, and snippets.

@sergebat
Forked from nagedev/SingletonScriptable.cs
Created October 18, 2018 18:39
Show Gist options
  • Save sergebat/fd45f53c14c50a0508aa7f95599bfb02 to your computer and use it in GitHub Desktop.
Save sergebat/fd45f53c14c50a0508aa7f95599bfb02 to your computer and use it in GitHub Desktop.
SingletonScriptable
using System.Linq;
using UnityEditor;
using UnityEngine;
public abstract class SingletonScriptable<T> : ScriptableObject where T : ScriptableObject
{
static T _instance = null;
public static T instance
{
get
{
if (!_instance)
{
_instance = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault();
if (!_instance)
{
string resourceName = typeof(T).ToString();
AssetDatabase.CreateAsset(CreateInstance<T>(), $"Assets/Resources/{resourceName}.asset");
_instance = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault();
}
}
return _instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment