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
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
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.id);
fsm.AddState(moveState).AddTranstion("ToRotate", rotateState.id);
fsm.ChangeState(rotateState.id); //设置初始状态

状态转换

fsm.SendMessage("ToMove");

DebugMode

fsm.debugMode = true;

关于子状态:

在状态类内部创建状态机可以创建子状态

关于ID

状态机使用状态对象的 this.GetType().name作为状态ID,你可以根据需要修改

简单演示Demo

./Demo/Demo.unity

Comments ( 0 )

Sign in for post a comment

About

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

Releases

No release

Gitee Metrics

Contributors

All

Activities

load more
can not load any more
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