Created
September 12, 2020 12:33
-
-
Save NeatWolf/0d56981856a16c027d725c2273e3644d to your computer and use it in GitHub Desktop.
Abstract class for making reload-proof singletons out of Uniy3D ScriptableObjects
This file contains 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 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