Unity c# 状态机的简单入门
状态机模式在unity中作用是非常大的,可以实现角色的移动和场景的跳转,包括一些动画的播放,在很多unity框架中也是很常见的,发散思维广阔,下面是简单的状态机的实现,有注释

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum StateType
{

Idle,Die,Move,

}

public abstract class State
{

protected StateManger state;public State (StateManger _sm){    state = _sm;}//进入方法public abstract void EnterState(); //离开方法public abstract void ExiState();//持续更新方法public abstract void UpdateState();

}
//站着状态
public class IdleState : State
{

public IdleState(StateManger state): (state){}public override void EnterState(){    Debug.Log("进入站着状态");}public override void ExiState(){    Debug.Log("离开站着状态");}public override void UpdateState(){    Debug.Log("等待站着状态");    if (Input .GetKey(KeyCode.M))    {        Debug.Log("按下咯");        state.ChangeState("Move");    }    if (Input.GetKey(KeyCode.D))    {        state.ChangeState("Die");    }}

}
//移动状态
public class MoveState : State
{

public MoveState(StateManger state): (state){}public override void EnterState(){    Debug.Log("进入移动状态");}public override void ExiState(){    Debug.Log("离开移动状态");}public override void UpdateState(){    Debug.Log("进入移动更新状态");    if (Input.GetKey(KeyCode.D))    {        state.ChangeState("Die");    }    if (Input.GetKey(KeyCode.I))    {        state.ChangeState("Idle");    }}

}
//死亡状态
public class DieState : State
{

public DieState(StateManger state) :  (state){}public override void EnterState(){    Debug.Log("进入死亡状态");}public override void ExiState(){    Debug.Log("离开死亡状态");}public override void UpdateState(){    Debug.Log("进入死亡更新状态");       if (Input.GetKey(KeyCode.I))    {        state.ChangeState("Idle");    }}

}
public class StateManger {

//字典存储状态Dictionary<string, State > dic = new Dictionary<string, State >();//当前状态State  currentstate;//注册状态public void Region(string statename,State  state){    //判断字典中是否存在这个键    if (!dic.ContainsKey(statename))    {        dic.Add(statename,state);    }}//设置默认状态public  void SetDefat(string statename){    //判断字典中是否存在这个状态    if (dic.ContainsKey(statename))    {        //存在就赋值给currentstate        currentstate = dic[statename];        //调用当前状态的进入(EnterState)方法        currentstate.EnterState();    }}//改变状态public  void ChangeState(string statename){    //判断字典中是否存在这个状态    if (dic.ContainsKey(statename))    {        //当前状态是否为空        if (currentstate!=null)        {            //调用上一个状态的离开方法            currentstate.ExiState();           //把取到的状态赋值给currentstate            currentstate = dic[statename];            //调用取到状态的进入方法            currentstate.EnterState();        }    }}//更新状态public void UpdateState(){    Debug.Log("更新状态");    if (currentstate!=null)    {        //当前状态的UpdateState方法        currentstate.UpdateState();    }}

}
public class FMSC : MonoBehaviour {

StateManger sm = new StateManger();// Use this for initializationvoid Start () {    //注册站着的方法    sm.Region("Idle",new IdleState(sm));    //注册死亡的方法    sm.Region("Die",new DieState(sm));    //注册移动的方法    sm.Region("Move",new MoveState(sm));    //设置默认状态    sm.SetDefat("Idle");}// Update is called once per  void Update () {    //持续调用当前状态的UpdateState方法    sm.UpdateState();}

}

每个状态都是有自己的方法,在每个状态有不同要做的事情,减少代码的耦合性

作者:憨豆人生
来源:CSDN
原文:https://blog.csdn.net/qq_40390815/article/details/90633826
版权声明:本文为博主原创文章,转载请附上博文链接!

收藏 打印