Unity State Macine


状态机

public interface IState
{
    void Enter();
    
    void Exit();
    
    void LogicUpdate();
    
    void PhysicsUpdate();
}
using System.Collections.Generic;
using UnityEngine;

/**
 * 状态机类
 *  支持所有的状态类,并对他们进行管理和切换
 *  负责进行当前状态的更新
 */
public class StateMachine : MonoBehaviour
{
    IState currentState;
    public Dictionary<System.Type, PlayerState> stateTable;
    void Update()
    {
        currentState.LogicUpdate();
    }

    private void FixedUpdate()
    {
        currentState.PhysicsUpdate();
    }


    protected void SwitchOn(IState newState)
    {
        currentState = newState;
        currentState.Enter();
    }

    public void SwitchState(IState newState)
    {
        currentState.Exit();
        SwitchOn(newState);
    }

    public void SwitchState(System.Type state)
    {
        currentState.Exit();
        SwitchOn(stateTable[state]);
    }
    
}

声明:冰醋酸|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Unity State Macine


一切随缘