Files
zeling_v2/Packages/com.kybernetik.animancer/Runtime/Utilities/FSM/StateBehaviour.cs
2026-05-08 11:04:00 +08:00

112 lines
4.6 KiB
C#

// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2026 Kybernetik //
using UnityEngine;
namespace Animancer.FSM
{
/// <summary>Base class for <see cref="MonoBehaviour"/> states to be used in a <see cref="StateMachine{TState}"/>.</summary>
/// <remarks>
/// <strong>Documentation:</strong>
/// <see href="https://kybernetik.com.au/animancer/docs/manual/fsm/state-types">
/// State Types</see>
/// </remarks>
/// https://kybernetik.com.au/animancer/api/Animancer.FSM/StateBehaviour
///
// [HelpURL(StateExtensions.APIDocumentationURL + nameof(StateBehaviour))]
public abstract class StateBehaviour : MonoBehaviour, IState
{
/************************************************************************************************************************/
/// <summary>[<see cref="IState.CanEnterState"/>]
/// Determines whether the <see cref="StateMachine{TState}"/> can enter this state.
/// Always returns true unless overridden.
/// </summary>
public virtual bool CanEnterState => true;
/// <summary>[<see cref="IState.CanExitState"/>]
/// Determines whether the <see cref="StateMachine{TState}"/> can exit this state.
/// Always returns true unless overridden.
/// </summary>
public virtual bool CanExitState => true;
/************************************************************************************************************************/
/// <summary>[<see cref="IState.OnEnterState"/>]
/// Asserts that this component isn't already enabled, then enables it.
/// </summary>
public virtual void OnEnterState()
{
AssertEnabledAndRepaintIfSelected(false, nameof(OnEnterState));
enabled = true;
}
/************************************************************************************************************************/
/// <summary>[<see cref="IState.OnExitState"/>]
/// Asserts that this component isn't already disabled, then disables it.
/// </summary>
public virtual void OnExitState()
{
if (this == null)
return;
AssertEnabledAndRepaintIfSelected(true, nameof(OnExitState));
enabled = false;
}
/************************************************************************************************************************/
#if UNITY_EDITOR
/// <summary>[Editor-Only]
/// Should the Inspector be repainted when a <see cref="StateBehaviour"/>
/// is enabled or disabled while it is selected?
/// </summary>
/// <remarks>Default is true.</remarks>
public static bool ForceRepaintOnEnableDisable { get; set; } = true;
private static double _LastRepaintTime;
#endif
/// <summary>[Assert-Conditional]
/// Asserts this <see cref="Behaviour.enabled"/>
/// and instructs the Unity Editor to repaint if this object is selected so the Inspector updates properly.
/// </summary>
[System.Diagnostics.Conditional("UNITY_ASSERTIONS")]
private void AssertEnabledAndRepaintIfSelected(bool expectEnabled, string callerName)
{
#if UNITY_ASSERTIONS
if (enabled != expectEnabled)
Debug.LogError(
$"{nameof(StateBehaviour)} was already {(expectEnabled ? "disabled" : "enabled")}" +
$" before {callerName}: {this}",
this);
#endif
#if UNITY_EDITOR
// Unity doesn't constantly repaint the Inspector if all the components are collapsed.
// So we can simply force it here to ensure that it shows the correct state being enabled.
if (ForceRepaintOnEnableDisable && UnityEditor.Selection.Contains(gameObject))
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
#endif
}
/************************************************************************************************************************/
#if UNITY_EDITOR
/// <summary>[Editor-Only] States start disabled and only the current state gets enabled at runtime.</summary>
/// <remarks>Called in Edit Mode whenever this script is loaded or a value is changed in the Inspector.</remarks>
protected virtual void OnValidate()
{
if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
return;
enabled = false;
}
#endif
/************************************************************************************************************************/
}
}