// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik //
#if ! UNITY_EDITOR
#pragma warning disable CS0612 // Type or member is obsolete (for Layers in Animancer Lite).
#pragma warning disable CS0618 // Type or member is obsolete (for Layers in Animancer Lite).
#endif
using UnityEngine;
namespace Animancer
{
/// An that cancels any fades and logs warnings when they occur.
///
///
/// This is useful for based characters since fading does nothing for them.
///
/// You can also set the to 0 so that you don't need to set it
/// manually on all your transitions.
///
///
///
/// [SerializeField] private AnimancerComponent _Animancer;
///
/// private void Awake()
/// {
/// // To only apply it only in the Unity Editor and Development Builds:
/// DontAllowFade.Assert(_Animancer);
///
/// // Or to apply it at all times:
/// _Animancer.Playable.RequireUpdate(new DontAllowFade());
/// }
///
///
/// https://kybernetik.com.au/animancer/api/Animancer/DontAllowFade
///
public class DontAllowFade : Key, IUpdatable
{
/************************************************************************************************************************/
/// [Assert-Conditional] Applies a to `animancer`.
[System.Diagnostics.Conditional(Strings.Assertions)]
public static void Assert(AnimancerPlayable animancer)
{
#if UNITY_EDITOR
var warnings = OptionalWarning.ProOnly.DisableTemporarily();
animancer.RequirePreUpdate(new DontAllowFade());
warnings.Enable();
#endif
}
/************************************************************************************************************************/
/// If the `node` is fading, this methods logs a warning (Assert-Only) and cancels the fade.
private static void Validate(AnimancerNode node)
{
if (node != null && node.FadeSpeed != 0)
{
#if UNITY_ASSERTIONS
Debug.LogWarning($"The following {node.GetType().Name} is fading even though " +
$"{nameof(DontAllowFade)} is active: {node.GetDescription()}",
node.Root.Component as Object);
#endif
node.Weight = node.TargetWeight;
}
}
/************************************************************************************************************************/
/// Calls on all layers and their .
void IUpdatable.Update()
{
var layers = AnimancerPlayable.Current.Layers;
for (int i = layers.Count - 1; i >= 0; i--)
{
var layer = layers[i];
Validate(layer);
Validate(layer.CurrentState);
}
}
/************************************************************************************************************************/
}
}