Skip to content

Instantly share code, notes, and snippets.

@blackbone
Created February 9, 2026 12:51
Show Gist options
  • Select an option

  • Save blackbone/7fbd97ae5ef0b3ef3e7165071db97c23 to your computer and use it in GitHub Desktop.

Select an option

Save blackbone/7fbd97ae5ef0b3ef3e7165071db97c23 to your computer and use it in GitHub Desktop.
Scriptable Singleton in a correct way
using System;
using System.Linq;
using UnityEngine;
public class ScriptableConfig<T> : ScriptableObject where T : ScriptableConfig<T>
{
private static T _instance;
public static T Instance
{
get
{
if (_instance != null) return _instance;
if (Application.isPlaying) LoadRuntime();
#if UNITY_EDITOR
else LoadEditor();
#endif
return _instance;
}
}
private static void LoadRuntime() => _instance = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault();
#if UNITY_EDITOR
private void Awake()
{
if (Application.isPlaying) return;
var preloads = UnityEditor.PlayerSettings.GetPreloadedAssets();
if (preloads.Contains(this)) return;
if (preloads.Any(p => p is T))
{
var path = UnityEditor.AssetDatabase.GetAssetPath(this);
UnityEditor.AssetDatabase.DeleteAsset(path);
DestroyImmediate(this);
return;
}
Array.Resize(ref preloads, preloads.Length + 1);
preloads[^1] = this;
UnityEditor.PlayerSettings.SetPreloadedAssets(preloads);
}
private static void LoadEditor()
{
if (Application.isPlaying) return;
_instance = UnityEditor.PlayerSettings.GetPreloadedAssets().FirstOrDefault(p => p is T) as T;
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment