using UnityEngine; namespace Ragdoll { // 单例的状态机 public abstract class RdSingletonStateMachine : RdStateMachine where T : RdState where K : new() { private static K _instance; private static readonly object Lock = new object(); public static K Instance { get { if (_instance == null) // 上锁,防止重复实例化 lock (Lock) { _instance ??= new K(); } return _instance; } } } public abstract class RdStateMachine where T : RdState { protected T currentState; protected abstract void Start(); public virtual T GetCurState() { return currentState; } public virtual void TransitionToState(T nextState) { Debug.Log("change state ->" + nextState.ToString()); // 退出当前状态 if (currentState != null) { currentState.ExitState(); } // 切换到下一个状态 currentState = nextState; // 进入下一个状态 if (currentState != null) { currentState.EnterState(); } } } public interface RdState { public abstract void EnterState(); public abstract void ExitState(); } }