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(http://www.amjmh.com)
{
Debug.Log("等待站着状态");
if (Input .GetKey(KeyCode.M))
{
Debug.Log("按下咯");
state.ChangeState("Move");
}
if (Input.GetKey(KeyCode.D))
{
state.ChangeState("Die");
}
}
}
---------------------