MixerChildFade.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Animancer
  5. {
  6. /// <summary>
  7. /// Fades the child weights of a <see cref="MixerState{TParameter}"/> to a new parameter value
  8. /// instead of fading the actual parameter.
  9. /// </summary>
  10. ///
  11. /// <remarks>
  12. /// <para></para>
  13. /// <strong>Documentation:</strong>
  14. /// <see href="https://kybernetik.com.au/animancer/docs/manual/blending/mixers/smoothing">
  15. /// Smoothing</see>
  16. /// <para></para>
  17. /// <strong>Example:</strong>
  18. /// Imagine a Linear Mixer with thresholds 0, 1, 2 and child states A, B, C.
  19. /// If you fade its Parameter from 0 to 1 the states would go from A to B to C.
  20. /// But if you use this system instead, the states would go directly from A to C.
  21. /// <h2>Usage</h2>
  22. /// <code>
  23. /// [SerializeField] private AnimancerComponent _Animancer;
  24. /// [SerializeField] private LinearMixerTransition _Mixer;
  25. ///
  26. /// public void FadeMixerTo(float parameter, float fadeDuration)
  27. /// {
  28. /// _Mixer.State.FadeChildWeights(parameter, fadeDuration);
  29. /// }
  30. /// </code></remarks>
  31. ///
  32. /// https://kybernetik.com.au/animancer/api/Animancer/MixerChildFade
  33. ///
  34. public static class MixerChildFade
  35. {
  36. /************************************************************************************************************************/
  37. private static readonly List<float>
  38. ChildWeights = new();
  39. /************************************************************************************************************************/
  40. /// <summary>
  41. /// Fades the child weights of a <see cref="MixerState{TParameter}"/> to a new parameter value instead of fading
  42. /// the actual parameter.
  43. /// </summary>
  44. /// <remarks>See <see cref="MixerChildFade"/> for a usage example.</remarks>
  45. public static void FadeChildWeights<TParameter>(
  46. this MixerState<TParameter> mixer,
  47. TParameter parameter,
  48. float fadeDuration)
  49. {
  50. ChildWeights.Clear();
  51. var childCount = mixer.ChildCount;
  52. for (int i = 0; i < childCount; i++)
  53. ChildWeights.Add(mixer.GetChild(i).Weight);
  54. mixer.Parameter = parameter;
  55. if (!mixer.RecalculateWeights())
  56. return;
  57. var mixerPlayable = mixer.Playable;
  58. for (int i = 0; i < childCount; i++)
  59. {
  60. var child = mixer.GetChild(i);
  61. mixerPlayable.SetChildWeight(child, ChildWeights[i]);
  62. child.StartFade(Math.Max(child.TargetWeight, float.Epsilon), fadeDuration);
  63. }
  64. }
  65. /************************************************************************************************************************/
  66. }
  67. }