TemporarySettings.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Animancer.Editor
  7. {
  8. /// <summary>[Editor-Only]
  9. /// Stores data which needs to survive assembly reloading (such as from script compilation), but can be discarded
  10. /// when the Unity Editor is closed.
  11. /// </summary>
  12. internal class TemporarySettings : ScriptableObject
  13. {
  14. /************************************************************************************************************************/
  15. #region Instance
  16. /************************************************************************************************************************/
  17. private static TemporarySettings _Instance;
  18. /// <summary>Finds an existing instance of this class or creates a new one.</summary>
  19. private static TemporarySettings Instance
  20. {
  21. get
  22. {
  23. if (_Instance == null)
  24. {
  25. var instances = Resources.FindObjectsOfTypeAll<TemporarySettings>();
  26. if (instances.Length > 0)
  27. {
  28. _Instance = instances[0];
  29. }
  30. else
  31. {
  32. _Instance = CreateInstance<TemporarySettings>();
  33. _Instance.hideFlags = HideFlags.HideAndDontSave | HideFlags.DontUnloadUnusedAsset;
  34. }
  35. }
  36. return _Instance;
  37. }
  38. }
  39. /************************************************************************************************************************/
  40. protected virtual void OnEnable()
  41. {
  42. OnEnableSelection();
  43. }
  44. protected virtual void OnDisable()
  45. {
  46. OnDisableSelection();
  47. }
  48. /************************************************************************************************************************/
  49. #endregion
  50. /************************************************************************************************************************/
  51. #region Event Selection
  52. /************************************************************************************************************************/
  53. private readonly Dictionary<Object, Dictionary<string, int>>
  54. ObjectToPropertyPathToSelectedEvent = new Dictionary<Object, Dictionary<string, int>>();
  55. /************************************************************************************************************************/
  56. public static int GetSelectedEvent(SerializedProperty property)
  57. {
  58. var instance = Instance;
  59. if (!instance.ObjectToPropertyPathToSelectedEvent.TryGetValue(property.serializedObject.targetObject, out var pathToSelection))
  60. return -1;
  61. else if (pathToSelection.TryGetValue(property.propertyPath, out var selection))
  62. return selection;
  63. else
  64. return -1;
  65. }
  66. /************************************************************************************************************************/
  67. public static void SetSelectedEvent(SerializedProperty property, int eventIndex)
  68. {
  69. var pathToSelection = GetOrCreatePathToSelection(property.serializedObject.targetObject);
  70. if (eventIndex >= 0)
  71. pathToSelection[property.propertyPath] = eventIndex;
  72. else
  73. pathToSelection.Remove(property.propertyPath);
  74. }
  75. /************************************************************************************************************************/
  76. private static Dictionary<string, int> GetOrCreatePathToSelection(Object obj)
  77. {
  78. var instance = Instance;
  79. if (!instance.ObjectToPropertyPathToSelectedEvent.TryGetValue(obj, out var pathToSelection))
  80. {
  81. instance.ObjectToPropertyPathToSelectedEvent.Add(obj,
  82. pathToSelection = new Dictionary<string, int>());
  83. }
  84. return pathToSelection;
  85. }
  86. /************************************************************************************************************************/
  87. [SerializeField] private Serialization.ObjectReference[] _EventSelectionObjects;
  88. [SerializeField] private string[] _EventSelectionPropertyPaths;
  89. [SerializeField] private int[] _EventSelectionIndices;
  90. /************************************************************************************************************************/
  91. private void OnDisableSelection()
  92. {
  93. var objects = new List<Serialization.ObjectReference>();
  94. var paths = new List<string>();
  95. var indices = new List<int>();
  96. foreach (var objectToSelection in ObjectToPropertyPathToSelectedEvent)
  97. {
  98. foreach (var pathToSelection in objectToSelection.Value)
  99. {
  100. objects.Add(objectToSelection.Key);
  101. paths.Add(pathToSelection.Key);
  102. indices.Add(pathToSelection.Value);
  103. }
  104. }
  105. _EventSelectionObjects = objects.ToArray();
  106. _EventSelectionPropertyPaths = paths.ToArray();
  107. _EventSelectionIndices = indices.ToArray();
  108. }
  109. /************************************************************************************************************************/
  110. private void OnEnableSelection()
  111. {
  112. if (_EventSelectionObjects == null ||
  113. _EventSelectionPropertyPaths == null ||
  114. _EventSelectionIndices == null)
  115. return;
  116. var count = _EventSelectionObjects.Length;
  117. if (count > _EventSelectionPropertyPaths.Length)
  118. count = _EventSelectionPropertyPaths.Length;
  119. if (count > _EventSelectionIndices.Length)
  120. count = _EventSelectionIndices.Length;
  121. for (int i = 0; i < count; i++)
  122. {
  123. var obj = _EventSelectionObjects[i];
  124. if (obj.IsValid())
  125. {
  126. var pathToSelection = GetOrCreatePathToSelection(obj);
  127. pathToSelection.Add(_EventSelectionPropertyPaths[i], _EventSelectionIndices[i]);
  128. }
  129. }
  130. }
  131. /************************************************************************************************************************/
  132. #endregion
  133. /************************************************************************************************************************/
  134. #region Preview Models
  135. /************************************************************************************************************************/
  136. [SerializeField]
  137. private List<GameObject> _PreviewModels;
  138. public static List<GameObject> PreviewModels
  139. {
  140. get
  141. {
  142. var instance = Instance;
  143. AnimancerEditorUtilities.RemoveMissingAndDuplicates(ref instance._PreviewModels);
  144. return instance._PreviewModels;
  145. }
  146. }
  147. /************************************************************************************************************************/
  148. #endregion
  149. /************************************************************************************************************************/
  150. }
  151. }
  152. #endif