DefaultValues.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System.Reflection;
  4. using UnityEditor;
  5. namespace Animancer.Editor
  6. {
  7. /// <summary>[Editor-Only] Utilities for using <see cref="DefaultValueAttribute"/>s.</summary>
  8. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/DefaultValues
  9. public static class DefaultValues
  10. {
  11. /************************************************************************************************************************/
  12. /// <summary>[Editor-Only]
  13. /// If the field represented by the `property` has a <see cref="DefaultValueAttribute"/>,
  14. /// this method sets the `value` to its <see cref="DefaultValueAttribute.Primary"/> value.
  15. /// If it was already at the value, it sets it to the <see cref="DefaultValueAttribute.Secondary"/>
  16. /// value instead. And if the field has no attribute, it uses the default for the type.
  17. /// </summary>
  18. public static void SetToDefault<T>(ref T value, SerializedProperty property)
  19. {
  20. var accessor = property.GetAccessor();
  21. var field = accessor.GetField(property);
  22. if (field == null)
  23. accessor.SetValue(property, null);
  24. else
  25. SetToDefault(ref value, field);
  26. }
  27. /************************************************************************************************************************/
  28. /// <summary>[Editor-Only]
  29. /// If the field represented by the `property` has a <see cref="DefaultValueAttribute"/>,
  30. /// this method sets the `value` to its <see cref="DefaultValueAttribute.Primary"/> value.
  31. /// If it was already at the value, it sets it to the <see cref="DefaultValueAttribute.Secondary"/>
  32. /// value instead. And if the field has no attribute, it uses the default for the type.
  33. /// </summary>
  34. public static void SetToDefault<T>(ref T value, FieldInfo field)
  35. {
  36. var defaults = field.GetAttribute<DefaultValueAttribute>();
  37. if (defaults != null)
  38. defaults.SetToDefault(ref value);
  39. else
  40. value = default;
  41. }
  42. /************************************************************************************************************************/
  43. /// <summary>[Editor-Only]
  44. /// Sets the `value` equal to the <see cref="DefaultValueAttribute.Primary"/> value.
  45. /// If it was already at the value, it sets it equal to the <see cref="DefaultValueAttribute.Secondary"/>
  46. /// value instead.
  47. /// </summary>
  48. public static void SetToDefault<T>(this DefaultValueAttribute attribute, ref T value)
  49. {
  50. var primary = attribute.Primary;
  51. if (!Equals(value, primary))
  52. {
  53. value = (T)primary;
  54. return;
  55. }
  56. var secondary = attribute.Secondary;
  57. if (secondary != null || !typeof(T).IsValueType)
  58. {
  59. value = (T)secondary;
  60. return;
  61. }
  62. }
  63. /************************************************************************************************************************/
  64. /// <summary>[Editor-Only]
  65. /// Sets the `value` equal to the `primary` value.
  66. /// If it was already at the value, it sets it equal to the `secondary` value instead.
  67. /// </summary>
  68. public static void SetToDefault<T>(ref T value, T primary, T secondary)
  69. {
  70. if (!Equals(value, primary))
  71. value = primary;
  72. else
  73. value = secondary;
  74. }
  75. /************************************************************************************************************************/
  76. }
  77. }
  78. #endif