Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@NeatWolf
Created September 12, 2020 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeatWolf/0d56981856a16c027d725c2273e3644d to your computer and use it in GitHub Desktop.
Save NeatWolf/0d56981856a16c027d725c2273e3644d to your computer and use it in GitHub Desktop.
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