UnitsAttribute.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer.Units
  4. {
  5. /// <summary>[Editor-Conditional]
  6. /// Causes a float field to display a suffix to indicate what kind of units the value represents as well as
  7. /// displaying it as several different fields which convert the value between different units.
  8. /// </summary>
  9. /// <remarks>
  10. /// <strong>Documentation:</strong>
  11. /// <see href="https://kybernetik.com.au/animancer/docs/manual/other/units">
  12. /// Units Attribute</see>
  13. /// </remarks>
  14. /// https://kybernetik.com.au/animancer/api/Animancer.Units/UnitsAttribute
  15. ///
  16. [System.Diagnostics.Conditional(Strings.UnityEditor)]
  17. public class UnitsAttribute : PropertyAttribute
  18. {
  19. /************************************************************************************************************************/
  20. /// <summary>The multipliers to convert from the field's actual value to each unit type.</summary>
  21. /// <remarks><c>valueInUnitX = valueInBaseUnits * Multipliers[x];</c></remarks>
  22. public readonly float[] Multipliers;
  23. /// <summary>The unit suffix to display at the end of the value in each field.</summary>
  24. public readonly string[] Suffixes;
  25. /// <summary>The index of the multiplier where the field stores its actual value.</summary>
  26. /// <remarks>The multiplier at this index must always be 1.</remarks>
  27. public readonly int UnitIndex;
  28. /************************************************************************************************************************/
  29. /// <summary>The validation rule applied to the value.</summary>
  30. public Validate.Value Rule { get; set; }
  31. /// <summary>Should the field have a toggle to set its value to <see cref="float.NaN"/>?</summary>
  32. public bool IsOptional { get; set; }
  33. /// <summary>The value to display if the actual value is <see cref="float.NaN"/>.</summary>
  34. public float DefaultValue { get; set; }
  35. /// <summary>Optional text to display instead of the regular fields when the value is <see cref="float.NaN"/>.</summary>
  36. public string DisabledText { get; set; }
  37. /************************************************************************************************************************/
  38. /// <summary>Creates a new <see cref="UnitsAttribute"/>.</summary>
  39. protected UnitsAttribute() { }
  40. /// <summary>Creates a new <see cref="UnitsAttribute"/>.</summary>
  41. public UnitsAttribute(string suffix)
  42. {
  43. Multipliers = new float[] { 1 };
  44. Suffixes = new string[] { suffix };
  45. }
  46. /// <summary>Creates a new <see cref="UnitsAttribute"/>.</summary>
  47. public UnitsAttribute(float[] multipliers, string[] suffixes, int unitIndex = 0)
  48. {
  49. Multipliers = multipliers;
  50. Suffixes = suffixes;
  51. UnitIndex = unitIndex;
  52. Debug.Assert(multipliers.Length == suffixes.Length,
  53. $"[Units] The {nameof(multipliers)} and {nameof(suffixes)} arrays have different lengths.");
  54. Debug.Assert((uint)UnitIndex < (uint)multipliers.Length,
  55. $"[Units] The {nameof(unitIndex)} is outside the {nameof(multipliers)} array.");
  56. }
  57. /************************************************************************************************************************/
  58. }
  59. }