SingletonScriptableObject.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace TheraBytes.BetterUi
  9. {
  10. public abstract class SingletonScriptableObject<T> : ScriptableObject
  11. where T : SingletonScriptableObject<T>
  12. {
  13. static T instance;
  14. static bool creatingInstance = false;
  15. public static T Instance
  16. {
  17. get
  18. {
  19. EnsureInstance();
  20. return instance;
  21. }
  22. }
  23. public static bool HasInstance { get { return instance != null; } }
  24. public static bool ScriptableObjectFileExists
  25. {
  26. get
  27. {
  28. if (HasInstance)
  29. return true;
  30. string filePath = GetFilePathWithExtention(true);
  31. return File.Exists(filePath);
  32. }
  33. }
  34. public static T EnsureInstance()
  35. {
  36. if (instance == null)
  37. {
  38. if (creatingInstance) // don't go here
  39. throw new Exception("Instance accessed during creation of instance.");
  40. creatingInstance = true;
  41. string filePath = GetFilePathWithExtention(false);
  42. string resourceFilePath = Path.GetFileNameWithoutExtension(
  43. filePath.Split(new string[] { "Resources" }, StringSplitOptions.None).Last());
  44. var obj = Resources.Load(resourceFilePath);
  45. instance = obj as T; // note: in the debugger it might be displayed as null (which is not the case)
  46. if (obj == null)
  47. {
  48. instance = CreateInstance<T>(); // note: in the debugger it might be displayed as null (which is not the case)
  49. #if UNITY_EDITOR && !UNITY_CLOUD_BUILD
  50. string completeFilePath = Path.Combine(Application.dataPath, filePath);
  51. string directory = Path.GetDirectoryName(completeFilePath);
  52. if (!Directory.Exists(directory))
  53. {
  54. Directory.CreateDirectory(directory);
  55. }
  56. UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/" + filePath);
  57. UnityEditor.AssetDatabase.Refresh();
  58. #else
  59. Debug.LogErrorFormat(
  60. "Could not find scriptable object of type '{0}'. Make sure it is instantiated inside Unity before building.",
  61. typeof(T));
  62. #endif
  63. }
  64. creatingInstance = false;
  65. }
  66. return instance;
  67. }
  68. private static string GetFilePathWithExtention(bool fullPath)
  69. {
  70. Type t = typeof(T);
  71. var prop = t.GetProperty("FilePath", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic);
  72. if (prop == null) throw new Exception("No static Property 'FilePath' in " + t.ToString());
  73. string filePath = prop.GetValue(null, null) as string;
  74. if (filePath == null) throw new Exception("static property 'FilePath' is not a string or null in " + t.ToString());
  75. if (!filePath.Contains("Resources")) throw new Exception("static property 'FilePath' must contain a Resources folder.");
  76. if (filePath.Contains("Plugins")) throw new Exception("static property 'FilePath' must not contain a Plugin folder.");
  77. if (!filePath.EndsWith(".asset"))
  78. filePath += ".asset";
  79. return (fullPath)
  80. ? Path.Combine(Application.dataPath, filePath)
  81. : filePath;
  82. }
  83. }
  84. }