TemporarySettings.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 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. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/TemporarySettings
  13. internal class TemporarySettings : ScriptableObject
  14. {
  15. /************************************************************************************************************************/
  16. #region Instance
  17. /************************************************************************************************************************/
  18. private static TemporarySettings _Instance;
  19. /// <summary>Finds an existing instance of this class or creates a new one.</summary>
  20. private static TemporarySettings Instance
  21. => AnimancerEditorUtilities.FindOrCreate(
  22. ref _Instance,
  23. HideFlags.HideAndDontSave | HideFlags.DontUnloadUnusedAsset);
  24. /************************************************************************************************************************/
  25. protected virtual void OnEnable()
  26. {
  27. OnEnableSelection();
  28. }
  29. protected virtual void OnDisable()
  30. {
  31. OnDisableSelection();
  32. }
  33. /************************************************************************************************************************/
  34. #endregion
  35. /************************************************************************************************************************/
  36. #region Event Selection
  37. /************************************************************************************************************************/
  38. private readonly Dictionary<Object, Dictionary<string, int>>
  39. ObjectToPropertyPathToSelectedEvent = new();
  40. /************************************************************************************************************************/
  41. public static int GetSelectedEvent(SerializedProperty property)
  42. {
  43. var instance = Instance;
  44. if (!instance.ObjectToPropertyPathToSelectedEvent.TryGetValue(property.serializedObject.targetObject, out var pathToSelection))
  45. return -1;
  46. else if (pathToSelection.TryGetValue(property.propertyPath, out var selection))
  47. return selection;
  48. else
  49. return -1;
  50. }
  51. /************************************************************************************************************************/
  52. public static void SetSelectedEvent(SerializedProperty property, int eventIndex)
  53. {
  54. var pathToSelection = GetOrCreatePathToSelection(property.serializedObject.targetObject);
  55. if (eventIndex >= 0)
  56. pathToSelection[property.propertyPath] = eventIndex;
  57. else
  58. pathToSelection.Remove(property.propertyPath);
  59. }
  60. /************************************************************************************************************************/
  61. private static Dictionary<string, int> GetOrCreatePathToSelection(Object obj)
  62. {
  63. var instance = Instance;
  64. if (!instance.ObjectToPropertyPathToSelectedEvent.TryGetValue(obj, out var pathToSelection))
  65. instance.ObjectToPropertyPathToSelectedEvent.Add(obj, pathToSelection = new());
  66. return pathToSelection;
  67. }
  68. /************************************************************************************************************************/
  69. [SerializeField] private Serialization.ObjectReference[] _EventSelectionObjects;
  70. [SerializeField] private string[] _EventSelectionPropertyPaths;
  71. [SerializeField] private int[] _EventSelectionIndices;
  72. /************************************************************************************************************************/
  73. private void OnDisableSelection()
  74. {
  75. var objects = new List<Serialization.ObjectReference>();
  76. var paths = new List<string>();
  77. var indices = new List<int>();
  78. foreach (var objectToSelection in ObjectToPropertyPathToSelectedEvent)
  79. {
  80. foreach (var pathToSelection in objectToSelection.Value)
  81. {
  82. objects.Add(objectToSelection.Key);
  83. paths.Add(pathToSelection.Key);
  84. indices.Add(pathToSelection.Value);
  85. }
  86. }
  87. _EventSelectionObjects = objects.ToArray();
  88. _EventSelectionPropertyPaths = paths.ToArray();
  89. _EventSelectionIndices = indices.ToArray();
  90. }
  91. /************************************************************************************************************************/
  92. private void OnEnableSelection()
  93. {
  94. if (_EventSelectionObjects == null ||
  95. _EventSelectionPropertyPaths == null ||
  96. _EventSelectionIndices == null)
  97. return;
  98. var count = _EventSelectionObjects.Length;
  99. if (count > _EventSelectionPropertyPaths.Length)
  100. count = _EventSelectionPropertyPaths.Length;
  101. if (count > _EventSelectionIndices.Length)
  102. count = _EventSelectionIndices.Length;
  103. for (int i = 0; i < count; i++)
  104. {
  105. var obj = _EventSelectionObjects[i];
  106. if (obj.IsValid())
  107. {
  108. var pathToSelection = GetOrCreatePathToSelection(obj);
  109. pathToSelection.Add(_EventSelectionPropertyPaths[i], _EventSelectionIndices[i]);
  110. }
  111. }
  112. }
  113. /************************************************************************************************************************/
  114. #endregion
  115. /************************************************************************************************************************/
  116. #region Preview Models
  117. /************************************************************************************************************************/
  118. [SerializeField]
  119. private List<GameObject> _PreviewModels;
  120. public static List<GameObject> PreviewModels
  121. {
  122. get
  123. {
  124. var instance = Instance;
  125. AnimancerEditorUtilities.RemoveMissingAndDuplicates(ref instance._PreviewModels);
  126. return instance._PreviewModels;
  127. }
  128. }
  129. /************************************************************************************************************************/
  130. #endregion
  131. /************************************************************************************************************************/
  132. }
  133. }
  134. #endif