Unity Game FrameWork—module usage—game flow

A flow is a finite state machine that runs through the game's entire lifecycle. Through the process, different game states are decoupled, so that each module can be managed separately. Different functions are independent of each other in different processes, so that many codes will not be squeezed into one script. Such as checking resource process, updating resource process, selecting server process, logging into server process, creating character process, game selection menu process, game play process, etc.
Create a process: derived from the ProcedureBase class, and implement your own process class
switching process: ChangeState<process class>(procedureOwner);
the life cycle of the process
The following code is the process base class ProcedureBase of UGF

using GameFramework.Fsm;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
namespace GameFramework.Procedure
{
    
    
    /// <summary>
    /// 流程基类。
    /// </summary>
    public abstract class ProcedureBase : FsmState<IProcedureManager>
    {
    
    
        /// <summary>
        /// 状态初始化时调用。
        /// </summary>
        /// <param name="procedureOwner">流程持有者。</param>
        public override void OnInit(ProcedureOwner procedureOwner)
        {
    
    
            base.OnInit(procedureOwner);
        }

        /// <summary>
        /// 进入状态时调用。
        /// </summary>
        /// <param name="procedureOwner">流程持有者。</param>
        public override void OnEnter(ProcedureOwner procedureOwner)
        {
    
    
            base.OnEnter(procedureOwner);
        }

        /// <summary>
        /// 状态轮询时调用。
        /// </summary>
        /// <param name="procedureOwner">流程持有者。</param>
        /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
        /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
        public override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
    
    
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
        }

        /// <summary>
        /// 离开状态时调用。
        /// </summary>
        /// <param name="procedureOwner">流程持有者。</param>
        /// <param name="isShutdown">是否是关闭状态机时触发。</param>
        public override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
    
    
            base.OnLeave(procedureOwner, isShutdown);
        }

        /// <summary>
        /// 状态销毁时调用。
        /// </summary>
        /// <param name="procedureOwner">流程持有者。</param>
        public override void OnDestroy(ProcedureOwner procedureOwner)
        {
    
    
            base.OnDestroy(procedureOwner);
        }
    }
}

It can be seen that this process executes the life cycle function, and the initialization, entry, exit, polling, and destruction of the process can help us complete the logic of this process.
case use

  1. Create the procedures ProcedureLaunch and ProcedureSplash, and realize the procedure function. As shown in the figure, when the ProcedureLaunch process runs for one frame, it jumps to the next process, ProcedureSplash.
    insert image description here
    insert image description here

  2. Check the processes we need, and uncheck the unused processes. EntranceProcedure is the first entry process when the game is started.
    insert image description here
    At this point, the process can be executed normally. We can add a print log to check whether the process is executed.

Guess you like

Origin blog.csdn.net/qq_37619255/article/details/129066679