AnimancerSettingsGroup.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Animancer.Editor
  7. {
  8. /// <summary>[Editor-Only] A static reference to a persistent setting stored in <see cref="AnimancerSettings"/>.</summary>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/AnimancerSettingsGroup_1
  10. public static class AnimancerSettingsGroup<T>
  11. where T : AnimancerSettingsGroup, new()
  12. {
  13. /************************************************************************************************************************/
  14. /// <summary>Gets or creates a <typeparamref name="T"/> in the <see cref="AnimancerSettings"/> asset.</summary>
  15. public static T Instance
  16. => AnimancerSettings.GetOrCreateData<T>();
  17. /************************************************************************************************************************/
  18. }
  19. /// <summary>Base class for groups of fields that can be serialized inside <see cref="AnimancerSettings"/>.</summary>
  20. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/AnimancerSettingsGroup
  21. [Serializable, InternalSerializableType]
  22. public abstract class AnimancerSettingsGroup : IComparable<AnimancerSettingsGroup>
  23. {
  24. /************************************************************************************************************************/
  25. private int _DataIndex = -1;
  26. private string _BasePropertyPath;
  27. /************************************************************************************************************************/
  28. /// <summary>The user-firendly name to display in the Inspector.</summary>
  29. public abstract string DisplayName { get; }
  30. /// <summary>The index to display this data at in the Inspector.</summary>
  31. public abstract int Index { get; }
  32. /************************************************************************************************************************/
  33. /// <summary>Sets the index used to find <see cref="SerializedProperty"/> instances for this group.</summary>
  34. internal void SetDataIndex(int index)
  35. {
  36. if (_DataIndex == index)
  37. return;
  38. _DataIndex = index;
  39. _BasePropertyPath = null;
  40. }
  41. /************************************************************************************************************************/
  42. /// <summary>Returns a <see cref="SerializedProperty"/> relative to the base of this group.</summary>
  43. protected SerializedProperty GetSerializedProperty(string propertyPath)
  44. => AnimancerSettings.GetSerializedProperty(_DataIndex, ref _BasePropertyPath, propertyPath);
  45. /************************************************************************************************************************/
  46. /// <summary>
  47. /// Draws a <see cref="EditorGUILayout.PropertyField(SerializedProperty, GUILayoutOption[])"/> for a
  48. /// property in this group.
  49. /// </summary>
  50. protected SerializedProperty DoPropertyField(string propertyPath)
  51. {
  52. var property = GetSerializedProperty(propertyPath);
  53. EditorGUILayout.PropertyField(property, true);
  54. return property;
  55. }
  56. /************************************************************************************************************************/
  57. /// <summary>Compares the <see cref="Index"/>.</summary>
  58. public int CompareTo(AnimancerSettingsGroup other)
  59. => Index.CompareTo(other.Index);
  60. /************************************************************************************************************************/
  61. }
  62. }
  63. #endif