Files
plugin-library/Assets/03.FiniteStateMachine/RunTime/PlayerFSMExample.cs
2025-03-21 18:34:26 +08:00

53 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerFSMExample : MonoBehaviour
{
private int controllerId;
private int idleStateId;
private float timer = 0.0f;
private FSMManager fSMManager;
void Start()
{
fSMManager = FSMInitialize.Init(gameObject.scene);
controllerId = fSMManager.CreateFSMController();
StateDateAction stateDateAction = new StateDateAction()
{
OnEnterAction = () =>
{
Debug.Log("进入状态");
},
UpdateAction = () =>
{
timer += Time.deltaTime;
Debug.Log("当前时间:" + timer);
},
OnExitAction = () =>
{
Debug.Log("离开状态");
}
};
idleStateId = fSMManager.CreateState(controllerId, stateDateAction);
}
void Update()
{
// 按下空格键切换到Running状态
if (Input.GetKeyDown(KeyCode.A))
{
fSMManager.EnterState(idleStateId);
}
if (Input.GetKeyDown(KeyCode.D))
{
fSMManager.ExitState(idleStateId);
}
}
void OnDestroy()
{
}
}