DontAllowFade.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 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="AnimancerGraph.DefaultFadeDuration"/> to 0 so that you don't need to set it
  11. /// manually on all your transitions.
  12. /// <para></para>
  13. /// <strong>Example:</strong>
  14. /// <code>
  15. /// [SerializeField] private AnimancerComponent _Animancer;
  16. ///
  17. /// protected virtual 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.Graph.RequireUpdate(new DontAllowFade());
  24. /// }
  25. /// </code></remarks>
  26. ///
  27. /// https://kybernetik.com.au/animancer/api/Animancer/DontAllowFade
  28. ///
  29. public class DontAllowFade : Updatable
  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(AnimancerGraph 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.Graph.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. public override void Update()
  57. {
  58. var layers = AnimancerGraph.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. }