Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

58 行
1.5 KiB

using NaughtyAttributes;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients.StateMachines
{
public class StateMachine : MonoBehaviour
{
public State DefaultState;
[ReorderableList]
public State[] States;
State m_CurrentState;
void Start()
{
foreach (var state in States)
state.gameObject.SetActive(false);
SetState(DefaultState.StateName);
}
public void SetState(string stateName)
{
State newState = States.First(o => o.StateName == stateName);
if(newState != null)
{
if (m_CurrentState != null)
{
// Call Exit Actions
Callable.Call(m_CurrentState.OnStateExit, gameObject);
// Then finally disable old state
m_CurrentState.gameObject.SetActive(false);
}
// Switch Active new state
newState.gameObject.SetActive(true);
// Then Set new current state
m_CurrentState = newState;
// Finally, call State enter
Callable.Call(m_CurrentState.OnStateEnter, gameObject);
}
}
public void Update()
{
if (m_CurrentState != null)
Callable.Call(m_CurrentState.OnStateUpdate, this.gameObject);
}
}
}