Instantly share code, notes, and snippets.

Embed
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;
}
}
}
@pepegich

This comment has been minimized.

pepegich commented Dec 7, 2018

FindObjectsOfTypeAll return null, if resource not loaded. you need load it via Resources.Load

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment