Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Abstract class for making reload-proof singletons out of Uniy3D ScriptableObjects
using System.Linq;
using UnityEngine;
/// <summary>
/// Abstract class for making reload-proof singletons out of ScriptableObjects
/// Returns the asset created on editor, null if there is none
/// Based on https://www.youtube.com/watch?v=VBA1QCoEAX4
/// </summary>
/// <typeparam name="T">Type of the singleton</typeparam>
public abstract class SingletonScriptableObject<T> : ScriptableObject where T : ScriptableObject {
static T _instance = null;
public static T Instance
{
get
{
if (!_instance)
_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