AnimancerSettings.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace Animancer.Editor
  8. {
  9. /// <summary>[Editor-Only] Persistent settings used by Animancer.</summary>
  10. /// <remarks>
  11. /// This asset automatically creates itself when first accessed.
  12. /// <para></para>
  13. /// The default location is <em>Packages/com.kybernetik.animancer/Code/Editor</em>, but you can freely move it
  14. /// (and the whole Animancer folder) anywhere in your project.
  15. /// <para></para>
  16. /// These settings can also be accessed via the Settings in the <see cref="Tools.AnimancerToolsWindow"/>
  17. /// (<c>Window/Animation/Animancer Tools</c>).
  18. /// </remarks>
  19. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/AnimancerSettings
  20. ///
  21. [AnimancerHelpUrl(typeof(AnimancerSettings))]
  22. public class AnimancerSettings : ScriptableObject
  23. {
  24. /************************************************************************************************************************/
  25. private static AnimancerSettings _Instance;
  26. /// <summary>
  27. /// Loads an existing <see cref="AnimancerSettings"/> if there is one anywhere in your project.
  28. /// Otherwise, creates a new one and saves it in the Assets folder.
  29. /// </summary>
  30. public static AnimancerSettings Instance
  31. {
  32. get
  33. {
  34. if (_Instance != null)
  35. return _Instance;
  36. _Instance = AnimancerEditorUtilities.FindAssetOfType<AnimancerSettings>();
  37. if (_Instance != null)
  38. return _Instance;
  39. _Instance = CreateInstance<AnimancerSettings>();
  40. _Instance.name = "Animancer Settings";
  41. _Instance.hideFlags = HideFlags.DontSaveInBuild;
  42. var path = $"Assets/{_Instance.name}.asset";
  43. path = AssetDatabase.GenerateUniqueAssetPath(path);
  44. AssetDatabase.CreateAsset(_Instance, path);
  45. return _Instance;
  46. }
  47. }
  48. /************************************************************************************************************************/
  49. /// <summary>Finds an existing instance of this asset anywhere in the project.</summary>
  50. [InitializeOnLoadMethod]
  51. private static void FindExistingInstance()
  52. {
  53. if (_Instance == null)
  54. _Instance = AnimancerEditorUtilities.FindAssetOfType<AnimancerSettings>();
  55. }
  56. /************************************************************************************************************************/
  57. private SerializedObject _SerializedObject;
  58. /// <summary>The <see cref="SerializedProperty"/> representing the <see cref="Instance"/>.</summary>
  59. public static SerializedObject SerializedObject
  60. => Instance._SerializedObject ?? (Instance._SerializedObject = new(Instance));
  61. /************************************************************************************************************************/
  62. private readonly List<Dictionary<string, SerializedProperty>>
  63. SerializedProperties = new();
  64. private static SerializedProperty GetSerializedProperty(int index, string propertyPath)
  65. {
  66. while (index >= Instance.SerializedProperties.Count)
  67. Instance.SerializedProperties.Add(null);
  68. var properties = Instance.SerializedProperties[index];
  69. properties ??= Instance.SerializedProperties[index] = new();
  70. if (!properties.TryGetValue(propertyPath, out var property))
  71. {
  72. property = SerializedObject.FindProperty(propertyPath);
  73. properties.Add(propertyPath, property);
  74. }
  75. return property;
  76. }
  77. /// <summary>Returns a <see cref="SerializedProperty"/> relative to the data at the specified `index`.</summary>
  78. public static SerializedProperty GetSerializedProperty(
  79. int index,
  80. ref string basePropertyPath,
  81. string propertyPath)
  82. {
  83. if (string.IsNullOrEmpty(basePropertyPath))
  84. basePropertyPath =
  85. $"{nameof(_Data)}{Serialization.ArrayDataPrefix}{index}{Serialization.ArrayDataSuffix}";
  86. if (string.IsNullOrEmpty(propertyPath))
  87. propertyPath = basePropertyPath;
  88. else
  89. propertyPath = $"{basePropertyPath}.{propertyPath}";
  90. return GetSerializedProperty(index, propertyPath);
  91. }
  92. /************************************************************************************************************************/
  93. [SerializeReference]
  94. private List<AnimancerSettingsGroup> _Data;
  95. /// <summary>Returns a stored item of the specified type or creates a new one if necessary.</summary>
  96. public static T GetOrCreateData<T>()
  97. where T : AnimancerSettingsGroup, new()
  98. {
  99. ref var data = ref Instance._Data;
  100. data ??= new();
  101. var index = AnimancerEditorUtilities.IndexOfType(Instance._Data, typeof(T));
  102. if (index >= 0)
  103. return (T)data[index];
  104. var newT = new T();
  105. newT.SetDataIndex(data.Count);
  106. data.Add(newT);
  107. SetDirty();
  108. return newT;
  109. }
  110. /************************************************************************************************************************/
  111. /// <summary>Calls <see cref="EditorUtility.SetDirty"/> on the <see cref="Instance"/>.</summary>
  112. public static new void SetDirty()
  113. => EditorUtility.SetDirty(_Instance);
  114. /************************************************************************************************************************/
  115. /// <summary>
  116. /// Ensures that there is an instance of each class derived from <see cref="AnimancerSettingsGroup"/>.
  117. /// </summary>
  118. protected virtual void OnEnable()
  119. {
  120. AnimancerEditorUtilities.InstantiateDerivedTypes(ref _Data);
  121. for (int i = 0; i < _Data.Count; i++)
  122. _Data[i].SetDataIndex(i);
  123. }
  124. /************************************************************************************************************************/
  125. /// <summary>A custom Inspector for <see cref="AnimancerSettings"/>.</summary>
  126. [CustomEditor(typeof(AnimancerSettings), true), CanEditMultipleObjects]
  127. public class Editor : UnityEditor.Editor
  128. {
  129. /************************************************************************************************************************/
  130. [NonSerialized]
  131. private SerializedProperty _Data;
  132. /************************************************************************************************************************/
  133. /// <summary>Called when this object is first loaded.</summary>
  134. protected virtual void OnEnable()
  135. {
  136. _Data = serializedObject.FindProperty(nameof(AnimancerSettings._Data));
  137. }
  138. /************************************************************************************************************************/
  139. /// <inheritdoc/>
  140. public override void OnInspectorGUI()
  141. {
  142. DoInfoGUI();
  143. DoOptionalWarningsGUI();
  144. serializedObject.Update();
  145. var count = _Data.arraySize;
  146. for (int i = 0; i < count; i++)
  147. DoDataGUI(_Data.GetArrayElementAtIndex(i), i);
  148. serializedObject.ApplyModifiedProperties();
  149. }
  150. /************************************************************************************************************************/
  151. /// <summary>
  152. /// If <c>true</c>, the next <see cref="OnInspectorGUI"/> will skip drawing the info panel.
  153. /// </summary>
  154. public static bool HideNextInfo { get; set; }
  155. private void DoInfoGUI()
  156. {
  157. if (HideNextInfo)
  158. {
  159. HideNextInfo = false;
  160. }
  161. else
  162. {
  163. EditorGUILayout.HelpBox(
  164. "Feel free to move this asset anywhere in your project." +
  165. "\n\nIt should generally not be in the Animancer folder" +
  166. " so that if you ever update Animancer you can delete that folder" +
  167. " without losing these settings." +
  168. "\n\nIf this asset is deleted, it will be automatically recreated" +
  169. " with default values when something needs it.",
  170. MessageType.Info);
  171. }
  172. }
  173. /************************************************************************************************************************/
  174. private void DoDataGUI(SerializedProperty property, int index)
  175. {
  176. if (property.managedReferenceValue is AnimancerSettingsGroup value)
  177. {
  178. DoHeading(value.DisplayName);
  179. var first = true;
  180. var depth = property.depth;
  181. while (property.NextVisible(first) && property.depth > depth)
  182. {
  183. first = false;
  184. EditorGUILayout.PropertyField(property, true);
  185. }
  186. }
  187. else
  188. {
  189. EditorGUILayout.BeginHorizontal();
  190. DoHeading("Missing Type");
  191. if (GUILayout.Button("X", AnimancerGUI.MiniButtonStyle))
  192. {
  193. var count = _Data.arraySize;
  194. _Data.DeleteArrayElementAtIndex(index);
  195. if (count == _Data.arraySize)
  196. _Data.DeleteArrayElementAtIndex(index);
  197. serializedObject.ApplyModifiedProperties();
  198. GUIUtility.ExitGUI();
  199. }
  200. EditorGUILayout.EndHorizontal();
  201. }
  202. }
  203. /************************************************************************************************************************/
  204. private void DoOptionalWarningsGUI()
  205. {
  206. DoHeading("Optional Warnings");
  207. EditorGUILayout.BeginHorizontal();
  208. using (var label = PooledGUIContent.Acquire("Disabled Warnings"))
  209. {
  210. EditorGUI.BeginChangeCheck();
  211. var value = EditorGUILayout.EnumFlagsField(label, Validate.PermanentlyDisabledWarnings);
  212. if (EditorGUI.EndChangeCheck())
  213. Validate.PermanentlyDisabledWarnings = (OptionalWarning)value;
  214. }
  215. if (GUILayout.Button("Help", EditorStyles.miniButton, AnimancerGUI.DontExpandWidth))
  216. Application.OpenURL(Strings.DocsURLs.OptionalWarning);
  217. EditorGUILayout.EndHorizontal();
  218. }
  219. /************************************************************************************************************************/
  220. private static GUIStyle _HeadingStyle;
  221. /// <summary>Draws a heading label.</summary>
  222. public static void DoHeading(string text)
  223. => GUILayout.Label(text, _HeadingStyle ??= new(EditorStyles.largeLabel)
  224. {
  225. fontSize = 18,
  226. });
  227. /************************************************************************************************************************/
  228. /// <summary>Creates the Project Settings page.</summary>
  229. [SettingsProvider]
  230. public static SettingsProvider CreateSettingsProvider()
  231. {
  232. UnityEditor.Editor editor = null;
  233. return new("Project/" + Strings.ProductName, SettingsScope.Project)
  234. {
  235. keywords = new HashSet<string>() { Strings.ProductName },
  236. guiHandler = searchContext =>
  237. {
  238. if (editor == null)
  239. editor = CreateEditor(Instance);
  240. HideNextInfo = true;
  241. editor.OnInspectorGUI();
  242. },
  243. };
  244. }
  245. /************************************************************************************************************************/
  246. }
  247. /************************************************************************************************************************/
  248. }
  249. }
  250. #endif