1 Star 0 Fork 0

熊峻玉 / UFSM状态机

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Clone or download
README.md 1.56 KB
Copy Edit Web IDE Raw Blame History
熊峻玉 authored 2020-12-31 00:11 . update README.md.

UFSM状态机

介绍

适合于Unity中使用的状态机,方便为一个MonoBehaviour子类添加状态控制,代码精简,使用简单

简单使用教程

1.控制类继承自FSMBehaviour

public class CubeController : FSMBehaviour<CubeController>

2.定义状态类

//旋转状态
class RotateState : State<CubeController>
{
    public override void OnUpdate(CubeController owner)
    {
        ower.transform.Rotate(Vector3.up*180* Time.deltaTime);
        if (Input.GetKeyDown(KeyCode.X))
        {
            ower.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
        }
        //也可以重写OnEnter和OnExit方法添加逻辑
    }
}
//旋转状态
class MoveState : State<CubeController>
{
    public override void OnUpdate(CubeController owner)
    {
        ower.transform. Translate(Vector3.forward * Time.deltaTime);
    }
}

3.创建状态对象

RotateState rotateState = new RotateState();
MoveState moveState = new MoveState();

4.添加状态并设置状态切换条件,再设置一个初始状态就可以跑起来了

//下面两行代码添加状态并设置该状态拥有的转换条件
fsm.AddState(rotateState).AddTranstion("ToMove", moveState.GUID);
fsm.AddState(moveState).AddTranstion("ToRotate", rotateState.GUID);
fsm.ChangeState(rotateState.GUID); //设置初始状态

状态转换

fsm.SendMessage("ToMove");

DebugMode

fsm.debugMode = true;

简单演示Demo

./Demo/Demo.unity

Comment ( 0 )

Sign in for post a comment

C#
1
https://gitee.com/xiongks/ufsm-state-machine.git
git@gitee.com:xiongks/ufsm-state-machine.git
xiongks
ufsm-state-machine
UFSM状态机
master

Search