一、使用流程图

二、使用方法
1、创建状态
csharp
//状态一
public class IdleState : FsmState<StateMgr>
{
protected override void OnInit(IFsm<StateMgr> fsm)
{
Debug.Log("IdleState初始化");
}
protected override void OnEnter(IFsm<StateMgr> fsm)
{
Debug.Log("进入Idle状态");
}
protected override void OnUpdate(IFsm<StateMgr> fsm, float elapseSeconds, float realElapseSeconds)
{
if (fsm.Owner.move)
{
ChangeState<MoveState>(fsm);
}
}
protected override void OnLeave(IFsm<StateMgr> fsm, bool isShutdown)
{
Debug.Log("离开Idle状态");
}
protected override void OnDestroy(IFsm<StateMgr> fsm)
{
Debug.Log("销毁Idle状态");
}
}
csharp
//状态二
public class MoveState : FsmState<StateMgr>
{
protected override void OnInit(IFsm<StateMgr> fsm)
{
Debug.Log("MoveState初始化");
}
protected override void OnEnter(IFsm<StateMgr> fsm)
{
Debug.Log("进入Move状态");
}
protected override void OnUpdate(IFsm<StateMgr> fsm, float elapseSeconds, float realElapseSeconds)
{
if (!fsm.Owner.move)
{
ChangeState<IdleState>(fsm);
}
}
protected override void OnLeave(IFsm<StateMgr> fsm, bool isShutdown)
{
Debug.Log("离开Move状态");
}
}
2、创建状态管理器,创建fsm,并根据条件进行状态切换
csharp
public class StateMgr : MonoBehaviour
{
public bool move=false;
private IFsm<StateMgr> m_fsm;
// Start is called before the first frame update
void Start()
{
m_fsm = GF.Fsm.CreateFsm("PlayerFSM", this, new CheckState(),new IdleState(), new MoveState());
m_fsm.Start<IdleState>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
move=!move;
}
}
}
三、注意点
1、创建状态机时,不能加入多个相同的状态,否则会报错
例如下面这个情况,加入了两个相同的new CheckState(),就会报错
m_fsm = GF.Fsm.CreateFsm("PlayerFSM", this, new CheckState(),new IdleState(), new CheckState());