DontAllowFade.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer
  4. {
  5. /// <summary>An <see cref="IUpdatable"/> that cancels any fades and logs warnings when they occur.</summary>
  6. ///
  7. /// <remarks>
  8. /// This is useful for <see cref="Sprite"/> based characters since fading does nothing for them.
  9. /// <para></para>
  10. /// You can also set the <see cref="AnimancerPlayable.DefaultFadeDuration"/> to 0 so that you don't need to set it
  11. /// manually on all your transitions.
  12. /// </remarks>
  13. ///
  14. /// <example><code>
  15. /// [SerializeField] private AnimancerComponent _Animancer;
  16. ///
  17. /// private void Awake()
  18. /// {
  19. /// // To only apply it only in the Unity Editor and Development Builds:
  20. /// DontAllowFade.Assert(_Animancer);
  21. ///
  22. /// // Or to apply it at all times:
  23. /// _Animancer.Playable.RequireUpdate(new DontAllowFade());
  24. /// }
  25. /// </code></example>
  26. ///
  27. /// https://kybernetik.com.au/animancer/api/Animancer/DontAllowFade
  28. ///
  29. public class DontAllowFade : Key, IUpdatable
  30. {
  31. /************************************************************************************************************************/
  32. /// <summary>[Assert-Conditional] Applies a <see cref="DontAllowFade"/> to `animancer`.</summary>
  33. [System.Diagnostics.Conditional(Strings.Assertions)]
  34. public static void Assert(AnimancerPlayable animancer)
  35. {
  36. #if UNITY_ASSERTIONS
  37. animancer.RequirePreUpdate(new DontAllowFade());
  38. #endif
  39. }
  40. /************************************************************************************************************************/
  41. /// <summary>If the `node` is fading, this methods logs a warning (Assert-Only) and cancels the fade.</summary>
  42. private static void Validate(AnimancerNode node)
  43. {
  44. if (node != null && node.FadeSpeed != 0)
  45. {
  46. #if UNITY_ASSERTIONS
  47. Debug.LogWarning($"The following {node.GetType().Name} is fading even though " +
  48. $"{nameof(DontAllowFade)} is active: {node.GetDescription()}",
  49. node.Root.Component as Object);
  50. #endif
  51. node.Weight = node.TargetWeight;
  52. }
  53. }
  54. /************************************************************************************************************************/
  55. /// <summary>Calls <see cref="Validate"/> on all layers and their <see cref="AnimancerLayer.CurrentState"/>.</summary>
  56. void IUpdatable.Update()
  57. {
  58. var layers = AnimancerPlayable.Current.Layers;
  59. for (int i = layers.Count - 1; i >= 0; i--)
  60. {
  61. var layer = layers[i];
  62. Validate(layer);
  63. Validate(layer.CurrentState);
  64. }
  65. }
  66. /************************************************************************************************************************/
  67. }
  68. }