// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik //
using UnityEngine;
namespace Animancer.FSM
{
/// Base class for states to be used in a .
///
/// Documentation: State Types
///
/// https://kybernetik.com.au/animancer/api/Animancer.FSM/StateBehaviour
///
[HelpURL(StateExtensions.APIDocumentationURL + nameof(StateBehaviour))]
public abstract class StateBehaviour : MonoBehaviour, IState
{
/************************************************************************************************************************/
/// []
/// Determines whether the can enter this state.
/// Always returns true unless overridden.
///
public virtual bool CanEnterState => true;
/// []
/// Determines whether the can exit this state.
/// Always returns true unless overridden.
///
public virtual bool CanExitState => true;
/************************************************************************************************************************/
/// []
/// Asserts that this component isn't already enabled, then enables it.
///
public virtual void OnEnterState()
{
#if UNITY_ASSERTIONS
if (enabled)
Debug.LogError($"{nameof(StateBehaviour)} was already enabled before {nameof(OnEnterState)}: {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.
else
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
#endif
enabled = true;
}
/************************************************************************************************************************/
/// []
/// Asserts that this component isn't already disabled, then disables it.
///
public virtual void OnExitState()
{
if (this == null)
return;
#if UNITY_ASSERTIONS
if (!enabled)
Debug.LogError($"{nameof(StateBehaviour)} was already disabled before {nameof(OnExitState)}: {this}", this);
#endif
enabled = false;
}
/************************************************************************************************************************/
#if UNITY_EDITOR
/// [Editor-Only] States start disabled and only the current state gets enabled at runtime.
/// Called in Edit Mode whenever this script is loaded or a value is changed in the Inspector.
protected virtual void OnValidate()
{
if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
return;
enabled = false;
}
#endif
/************************************************************************************************************************/
}
}