// Animancer // Copyright 2020 Kybernetik // #if UNITY_EDITOR using System.Collections.Generic; using UnityEditor; using UnityEngine; namespace Animancer.Editor { /// [Editor-Only] /// Stores data which needs to survive assembly reloading (such as from script compilation), but can be discarded /// when the Unity Editor is closed. /// internal sealed class TemporarySettings : ScriptableObject { /************************************************************************************************************************/ #region Instance /************************************************************************************************************************/ private static TemporarySettings _Instance; /// /// Finds an existing instance of this class or creates a new one. /// public static TemporarySettings Instance { get { if (_Instance == null) { var instances = Resources.FindObjectsOfTypeAll(); if (instances.Length > 0) { _Instance = instances[0]; } else { _Instance = CreateInstance(); _Instance.hideFlags = HideFlags.HideAndDontSave | HideFlags.DontUnloadUnusedAsset; } } return _Instance; } } /************************************************************************************************************************/ private void OnEnable() { OnEnableSelection(); } private void OnDisable() { OnDisableSelection(); } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Event Selection /************************************************************************************************************************/ private readonly Dictionary> ObjectToPropertyPathToSelectedEvent = new Dictionary>(); /************************************************************************************************************************/ public int GetSelectedEvent(SerializedProperty property) { Dictionary pathToSelection; if (!ObjectToPropertyPathToSelectedEvent.TryGetValue(property.serializedObject.targetObject, out pathToSelection)) return -1; int selection; if (pathToSelection.TryGetValue(property.propertyPath, out selection)) return selection; else return -1; } /************************************************************************************************************************/ public void SetSelectedEvent(SerializedProperty property, int eventIndex) { var pathToSelection = GetOrCreatePathToSelection(property.serializedObject.targetObject); if (eventIndex >= 0) pathToSelection[property.propertyPath] = eventIndex; else pathToSelection.Remove(property.propertyPath); } /************************************************************************************************************************/ private Dictionary GetOrCreatePathToSelection(Object obj) { Dictionary pathToSelection; if (!ObjectToPropertyPathToSelectedEvent.TryGetValue(obj, out pathToSelection)) { ObjectToPropertyPathToSelectedEvent.Add(obj, pathToSelection = new Dictionary()); } return pathToSelection; } /************************************************************************************************************************/ [SerializeField] private Serialization.ObjectReference[] _EventSelectionObjects; [SerializeField] private string[] _EventSelectionPropertyPaths; [SerializeField] private int[] _EventSelectionIndices; /************************************************************************************************************************/ private void OnDisableSelection() { var objects = new List(); var paths = new List(); var indices = new List(); foreach (var objectToSelection in ObjectToPropertyPathToSelectedEvent) { foreach (var pathToSelection in objectToSelection.Value) { objects.Add(objectToSelection.Key); paths.Add(pathToSelection.Key); indices.Add(pathToSelection.Value); } } _EventSelectionObjects = objects.ToArray(); _EventSelectionPropertyPaths = paths.ToArray(); _EventSelectionIndices = indices.ToArray(); } /************************************************************************************************************************/ private void OnEnableSelection() { if (_EventSelectionObjects == null || _EventSelectionPropertyPaths == null || _EventSelectionIndices == null) return; var count = _EventSelectionObjects.Length; if (count > _EventSelectionPropertyPaths.Length) count = _EventSelectionPropertyPaths.Length; if (count > _EventSelectionIndices.Length) count = _EventSelectionIndices.Length; for (int i = 0; i < count; i++) { var obj = _EventSelectionObjects[i]; if (obj.IsValid()) { var pathToSelection = GetOrCreatePathToSelection(obj); pathToSelection.Add(_EventSelectionPropertyPaths[i], _EventSelectionIndices[i]); } } } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ } } #endif