PreloadedProjectSettings.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. #if UNITY_EDITOR
  6. using System.IO;
  7. using UnityEditor;
  8. using UnityEditor.Build;
  9. using UnityEditor.Build.Reporting;
  10. #endif
  11. namespace Coffee.UIParticleInternal
  12. {
  13. public abstract class PreloadedProjectSettings : ScriptableObject
  14. #if UNITY_EDITOR
  15. {
  16. private class Postprocessor : AssetPostprocessor
  17. {
  18. private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___, string[] ____)
  19. {
  20. Initialize();
  21. }
  22. }
  23. private class PreprocessBuildWithReport : IPreprocessBuildWithReport
  24. {
  25. int IOrderedCallback.callbackOrder => 0;
  26. void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report)
  27. {
  28. Initialize();
  29. }
  30. }
  31. private static void Initialize()
  32. {
  33. foreach (var t in TypeCache.GetTypesDerivedFrom(typeof(PreloadedProjectSettings<>)))
  34. {
  35. var defaultSettings = GetDefaultSettings(t);
  36. if (!defaultSettings)
  37. {
  38. // When create a new instance, automatically set it as default settings.
  39. defaultSettings = CreateInstance(t) as PreloadedProjectSettings;
  40. SetDefaultSettings(defaultSettings);
  41. }
  42. else if (GetPreloadedSettings(t).Length != 1)
  43. {
  44. SetDefaultSettings(defaultSettings);
  45. }
  46. if (defaultSettings)
  47. {
  48. defaultSettings.OnInitialize();
  49. }
  50. }
  51. }
  52. protected static string GetDefaultName(Type type, bool nicify)
  53. {
  54. var typeName = type.Name;
  55. return nicify
  56. ? ObjectNames.NicifyVariableName(typeName)
  57. : typeName;
  58. }
  59. private static Object[] GetPreloadedSettings(Type type)
  60. {
  61. return PlayerSettings.GetPreloadedAssets()
  62. .Where(x => x && x.GetType() == type)
  63. .ToArray();
  64. }
  65. protected static PreloadedProjectSettings GetDefaultSettings(Type type)
  66. {
  67. return GetPreloadedSettings(type).FirstOrDefault() as PreloadedProjectSettings
  68. ?? AssetDatabase.FindAssets($"t:{nameof(PreloadedProjectSettings)}")
  69. .Select(AssetDatabase.GUIDToAssetPath)
  70. .Select(AssetDatabase.LoadAssetAtPath<PreloadedProjectSettings>)
  71. .FirstOrDefault(x => x && x.GetType() == type);
  72. }
  73. protected static void SetDefaultSettings(PreloadedProjectSettings asset)
  74. {
  75. if (!asset) return;
  76. var type = asset.GetType();
  77. if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(asset)))
  78. {
  79. if (!AssetDatabase.IsValidFolder("Assets/ProjectSettings"))
  80. {
  81. AssetDatabase.CreateFolder("Assets", "ProjectSettings");
  82. }
  83. var assetPath = $"Assets/ProjectSettings/{GetDefaultName(type, false)}.asset";
  84. assetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
  85. if (!File.Exists(assetPath))
  86. {
  87. AssetDatabase.CreateAsset(asset, assetPath);
  88. asset.OnCreateAsset();
  89. }
  90. }
  91. var preloadedAssets = PlayerSettings.GetPreloadedAssets();
  92. var projectSettings = GetPreloadedSettings(type);
  93. PlayerSettings.SetPreloadedAssets(preloadedAssets
  94. .Where(x => x)
  95. .Except(projectSettings.Except(new[] { asset }))
  96. .Append(asset)
  97. .Distinct()
  98. .ToArray());
  99. AssetDatabase.Refresh();
  100. }
  101. protected virtual void OnCreateAsset()
  102. {
  103. }
  104. protected virtual void OnInitialize()
  105. {
  106. }
  107. }
  108. #else
  109. {
  110. }
  111. #endif
  112. public abstract class PreloadedProjectSettings<T> : PreloadedProjectSettings
  113. where T : PreloadedProjectSettings<T>
  114. {
  115. private static T s_Instance;
  116. #if UNITY_EDITOR
  117. private string _jsonText;
  118. public static bool hasInstance => s_Instance;
  119. public static T instance
  120. {
  121. get
  122. {
  123. if (s_Instance) return s_Instance;
  124. s_Instance = GetDefaultSettings(typeof(T)) as T;
  125. if (s_Instance) return s_Instance;
  126. s_Instance = CreateInstance<T>();
  127. if (!s_Instance)
  128. {
  129. s_Instance = null;
  130. return s_Instance;
  131. }
  132. SetDefaultSettings(s_Instance);
  133. return s_Instance;
  134. }
  135. }
  136. private void OnPlayModeStateChanged(PlayModeStateChange state)
  137. {
  138. switch (state)
  139. {
  140. case PlayModeStateChange.ExitingEditMode:
  141. _jsonText = EditorJsonUtility.ToJson(this);
  142. break;
  143. case PlayModeStateChange.ExitingPlayMode:
  144. if (_jsonText != null)
  145. {
  146. EditorJsonUtility.FromJsonOverwrite(_jsonText, this);
  147. _jsonText = null;
  148. }
  149. break;
  150. }
  151. }
  152. #else
  153. public static T instance => s_Instance ? s_Instance : s_Instance = CreateInstance<T>();
  154. #endif
  155. /// <summary>
  156. /// This function is called when the object becomes enabled and active.
  157. /// </summary>
  158. protected virtual void OnEnable()
  159. {
  160. #if UNITY_EDITOR
  161. var isDefaultSettings = !s_Instance || s_Instance == this || GetDefaultSettings(typeof(T)) == this;
  162. if (!isDefaultSettings)
  163. {
  164. DestroyImmediate(this, true);
  165. return;
  166. }
  167. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  168. #endif
  169. if (s_Instance) return;
  170. s_Instance = this as T;
  171. }
  172. /// <summary>
  173. /// This function is called when the behaviour becomes disabled.
  174. /// </summary>
  175. protected virtual void OnDisable()
  176. {
  177. #if UNITY_EDITOR
  178. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  179. #endif
  180. if (s_Instance != this) return;
  181. s_Instance = null;
  182. }
  183. #if UNITY_EDITOR
  184. protected sealed class PreloadedProjectSettingsProvider : SettingsProvider
  185. {
  186. private Editor _editor;
  187. private PreloadedProjectSettings<T> _target;
  188. public PreloadedProjectSettingsProvider(string path) : base(path, SettingsScope.Project)
  189. {
  190. }
  191. public override void OnGUI(string searchContext)
  192. {
  193. if (!_target)
  194. {
  195. if (_editor)
  196. {
  197. DestroyImmediate(_editor);
  198. _editor = null;
  199. }
  200. _target = instance;
  201. _editor = Editor.CreateEditor(_target);
  202. }
  203. _editor.OnInspectorGUI();
  204. }
  205. }
  206. #endif
  207. }
  208. }