Unity c# 狀態機的簡單入門

Unity c# 狀態機的簡單入門
狀態機模式在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();
}

}

每個狀態都是有自己的方法,在每個狀態有不同要做的事情,減少代碼的耦合性

作者:憨豆人生
來源:CSDN
原文:https://blog.csdn.net/qq_40390815/article/details/90633826
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章