// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // #if UNITY_EDITOR using UnityEditor; namespace Animancer.Editor { /// [Editor-Only] A wrapper around a representing an array field. public class SerializedArrayProperty { /************************************************************************************************************************/ private SerializedProperty _Property; /// The target property. public SerializedProperty Property { get => _Property; set { _Property = value; Refresh(); } } /************************************************************************************************************************/ private string _Path; /// The cached of the . public string Path => _Path ?? (_Path = Property.propertyPath); /************************************************************************************************************************/ private int _Count; /// The cached of the . public int Count { get => _Count; set => Property.arraySize = _Count = value; } /************************************************************************************************************************/ private bool _HasMultipleDifferentValues; private bool _GotHasMultipleDifferentValues; /// The cached of the . public bool HasMultipleDifferentValues { get { if (!_GotHasMultipleDifferentValues) { _GotHasMultipleDifferentValues = true; _HasMultipleDifferentValues = Property.hasMultipleDifferentValues; } return _HasMultipleDifferentValues; } } /************************************************************************************************************************/ /// Updates the cached and . public void Refresh() { _Path = null; _Count = _Property != null ? _Property.arraySize : 0; _GotHasMultipleDifferentValues = false; } /************************************************************************************************************************/ /// Calls on the . /// /// Returns null if the element is not actually a child of the , which can happen /// if multiple objects are selected with different array sizes. /// public SerializedProperty GetElement(int index) { var element = Property.GetArrayElementAtIndex(index); if (!HasMultipleDifferentValues || element.propertyPath.StartsWith(Path)) return element; else return null; } /************************************************************************************************************************/ } } #endif