UI state machine in Unity

using System;
using System.Collections.Generic;
using FrameWork;
using UnityEngine;

public abstract class StateSystem<T> : MonoBehaviour
{
    
    
    /// <summary>
    /// 状态机
    /// </summary>
    protected StateMachine<T> mState;

    /// <summary>
    /// 状态栈(可用于返回使用)
    /// </summary>
    protected Stack<T> mStateStack;

    private void Awake()
    {
    
    
        mState = new StateMachine<T>();
        mStateStack = new Stack<T>();

        initialize();
        initStates();
    }

    protected virtual void initialize()
    {
    
    
    }

    protected virtual void initStates()
    {
    
    
    }

    public void OnEnable()
    {
    
    
        mState.OnStart();
    }

    public virtual void Update()
    {
    
    
        mState.OnUpdate();
    }

    public void OnDisable()
    {
    
    
        mState.OnStop();
    }

    /// <summary>
    /// 切换状态
    /// </summary>
    /// <param name="state">欲切换的状态</param>
    /// <param name="saveState">是否保存上一个状态</param>
    public void ChangeState(T state, bool saveState = false)
    {
    
    
        try
        {
    
    
            if (mState.CurrentState.Equals(state))
            {
    
    
                return;
            }

            if (saveState)
            {
    
    
                mStateStack.Push(mState.CurrentState);
            }

            Debug.LogFormat("切换状态:{0} -> {1}", mState.CurrentState, state);
            mState.ChangeState(state);
        }
        catch (Exception e)
        {
    
    
            Debug.Log(e.Message);
            throw;
        }
    }

    /// <summary>
    /// 当前状态
    /// </summary>
    public T currentState
    {
    
    
        get {
    
     return mState.CurrentState; }
    }

    /// <summary>
    /// 返回到上一个保存的状态
    /// </summary>
    /// <returns></returns>
    public bool BackState()
    {
    
    
        if (mStateStack.Count == 0)
        {
    
    
            return false;
        }

        T lastState = mStateStack.Pop();
        mStateStack.Clear();
        ChangeState(lastState);
        return true;
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

namespace FrameWork
{
    
    
    public class StateMachine<T>
    {
    
    
        private class State
        {
    
    
            public readonly T label;
            public readonly Action onStart;
            public readonly Func<T> onUpdate;
            public readonly Action onStop;

            public State(T label, Action onStart, Func<T> onUpdate, Action onStop)
            {
    
    
                this.label = label;
                this.onStart = onStart;
                this.onUpdate = onUpdate;
                this.onStop = onStop;
            }
        }

        /// <summary>
        /// 状态集合
        /// </summary>
        private readonly Dictionary<T, State> mStateDictionary;

        private State mCurrentState;

        /// <summary>
        /// 上一个状态
        /// </summary>
        private State mPreState;

        /// <summary>
        /// 进入的状态名称
        /// </summary>
        private T mEnterState;

        /// <summary>
        /// 当前状态的label
        /// </summary>
        public T CurrentState
        {
    
    
            get {
    
     return mCurrentState.label; }
            set {
    
     ChangeState(value); }
        }

        /// <summary>
        /// 返回上一状态label
        /// </summary>
        public T PreState => mPreState.label;

        public bool IsRunning
        {
    
    
            get {
    
     return CurrentState != null; }
        }

        public StateMachine()
        {
    
    
            mStateDictionary = new Dictionary<T, State>();
        }

        public void OnStart()
        {
    
    
            if (mStateDictionary.Count == 0)
            {
    
    
                return;
            }

            mCurrentState = mStateDictionary[mEnterState];
            if (mCurrentState != null && mCurrentState.onStart != null)
            {
    
    
                mCurrentState.onStart();
            }
        }

        public void OnUpdate()
        {
    
    
            if (mCurrentState != null && mCurrentState.onUpdate != null)
            {
    
    
                mCurrentState.onUpdate();
            }
        }

        public void OnStop()
        {
    
    
            if (mCurrentState != null && mCurrentState.onStop != null)
            {
    
    
                mCurrentState.onStop();
            }
        }

        /// <summary>
        /// 添加状态
        /// </summary>
        /// <param name="label">状态</param>
        /// <param name="onStart">在状态开始时调用</param>
        /// <param name="onUpdate">在状态期间持续调用</param>
        /// <param name="onStop">在状态结束时调用</param>
        public void AddState(T label, Action onStart, Func<T> onUpdate, Action onStop)
        {
    
    
            State state = new State(label, onStart, onUpdate, onStop);
            mStateDictionary[label] = state;
            if (mCurrentState == null) //第一个添加的状态为初始状态
            {
    
    
                mCurrentState = state;
                mEnterState = mCurrentState.label;
            }
        }


        /// <summary>
        /// 切换状态
        /// </summary>
        /// <param name="newLabel"></param>
        public void ChangeState(T newLabel)
        {
    
    
            if (mCurrentState != null && mCurrentState.onStop != null)
            {
    
    
                mCurrentState.onStop();
            }

            mPreState = mCurrentState;
            mCurrentState = mStateDictionary[newLabel];

            if (mCurrentState != null && mCurrentState.onStart != null)
            {
    
    
                mCurrentState.onStart();
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_40028144/article/details/112983202