状态机模式在unity中作用是非常大的,可以实现角色的移动和场景的跳转,包括一些动画的播放,在很多unity框架中也是很常见的,发散思维广阔,下面是简单的状态机的实现,有注释
using System.Collections;
using System.Collections.Generic;using UnityEngine;public enum StateType{ Idle, Die, Move,}public abstract class StateObject
{ protected StateManger state; public StateObject(StateManger _sm) { state = _sm; } //进入方法 public abstract void EnterState(); //离开方法 public abstract void ExiState(); //持续更新方法 public abstract void UpdateState();}//站着状态public class IdleState : StateObject{ public IdleState(StateManger state):base(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 : StateObject{ public MoveState(StateManger state):base(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 : StateObject{ public DieState(StateManger state) : base(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, StateObject> dic = new Dictionary<string, StateObject>(); //当前状态 StateObject currentstate; //注册状态 public void Region(string statename,StateObject 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 initialization void 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 frame void Update () { //持续调用当前状态的UpdateState方法 sm.UpdateState(); }}1
23456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189每个状态都是有自己的方法,在每个状态有不同要做的事情,减少代码的耦合性
---------------------