-
-
Save sergebat/fd45f53c14c50a0508aa7f95599bfb02 to your computer and use it in GitHub Desktop.
SingletonScriptable
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 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