CustomFade.Curve.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer
  4. {
  5. /// https://kybernetik.com.au/animancer/api/Animancer/CustomFade
  6. ///
  7. public partial class CustomFade
  8. {
  9. /************************************************************************************************************************/
  10. /// <summary>Modify the current fade to use the specified `curve` to calculate the weight.</summary>
  11. /// <example>See <see cref="CustomFade"/>.</example>
  12. /// <remarks>The `curve` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  13. public static void Apply(AnimancerComponent animancer, AnimationCurve curve)
  14. => Apply(animancer.States.Current, curve);
  15. /// <summary>Modify the current fade to use the specified `curve` to calculate the weight.</summary>
  16. /// <example>See <see cref="CustomFade"/>.</example>
  17. /// <remarks>The `curve` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  18. public static void Apply(AnimancerPlayable animancer, AnimationCurve curve)
  19. => Apply(animancer.States.Current, curve);
  20. /// <summary>Modify the current fade to use the specified `curve` to calculate the weight.</summary>
  21. /// <example>See <see cref="CustomFade"/>.</example>
  22. /// <remarks>The `curve` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  23. public static void Apply(AnimancerState state, AnimationCurve curve)
  24. => Curve.Acquire(curve).Apply(state);
  25. /// <summary>Modify the current fade to use the specified `curve` to calculate the weight.</summary>
  26. /// <example>See <see cref="CustomFade"/>.</example>
  27. /// <remarks>The `curve` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  28. public static void Apply(AnimancerNode node, AnimationCurve curve)
  29. => Curve.Acquire(curve).Apply(node);
  30. /************************************************************************************************************************/
  31. /// <summary>A <see cref="CustomFade"/> which uses an <see cref="AnimationCurve"/> to calculate the weight.</summary>
  32. /// <example>See <see cref="CustomFade"/>.</example>
  33. private class Curve : CustomFade
  34. {
  35. /************************************************************************************************************************/
  36. private AnimationCurve _Curve;
  37. /************************************************************************************************************************/
  38. public static Curve Acquire(AnimationCurve curve)
  39. {
  40. if (curve == null)
  41. {
  42. OptionalWarning.CustomFadeNotNull.Log($"{nameof(curve)} is null.");
  43. return null;
  44. }
  45. var fade = ObjectPool<Curve>.Acquire();
  46. fade._Curve = curve;
  47. return fade;
  48. }
  49. /************************************************************************************************************************/
  50. protected override float CalculateWeight(float progress) => _Curve.Evaluate(progress);
  51. /************************************************************************************************************************/
  52. protected override void Release() => ObjectPool<Curve>.Release(this);
  53. /************************************************************************************************************************/
  54. }
  55. }
  56. }