2025-04-10 11:20:22 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Stary.Evo.FiniteStateMachine
|
|
|
|
|
|
{
|
2025-04-10 11:35:40 +08:00
|
|
|
|
public static class FSMExtension
|
2025-04-10 11:20:22 +08:00
|
|
|
|
{
|
2025-04-10 11:27:16 +08:00
|
|
|
|
private static Dictionary<string, AbstractFSMController> _fsmControllers =
|
|
|
|
|
|
new Dictionary<string, AbstractFSMController>();
|
2025-04-10 11:20:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建FSMController
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>返回创建的FSMController的识别Int值</returns>
|
2025-04-10 11:27:16 +08:00
|
|
|
|
public static void CreateFSMController<T>(Transform parent) where T : AbstractFSMController
|
2025-04-10 11:20:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (_fsmControllers.ContainsKey(typeof(T).Name))
|
|
|
|
|
|
{
|
2025-04-10 11:57:13 +08:00
|
|
|
|
Debug.LogError($"UnityEvo:FSM控制器已存在!!!_key:{typeof(T).Name}");
|
2025-04-10 11:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
GameObject newController = new GameObject($"{typeof(T).Name}");
|
|
|
|
|
|
newController.transform.SetParent(parent);
|
|
|
|
|
|
var controller = newController.AddComponent<T>();
|
|
|
|
|
|
|
|
|
|
|
|
_fsmControllers.Add(typeof(T).Name, controller);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 销毁指定FSMController
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="controllerId"></param>
|
2025-04-10 11:27:16 +08:00
|
|
|
|
public static void DestroyFSMController<T>() where T : AbstractFSMController
|
2025-04-10 11:20:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (_fsmControllers.TryGetValue(typeof(T).Name, out var controller))
|
|
|
|
|
|
{
|
|
|
|
|
|
GameObject.Destroy(controller.gameObject);
|
|
|
|
|
|
_fsmControllers.Remove(typeof(T).Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-04-10 11:57:13 +08:00
|
|
|
|
Debug.LogError("UnityEvo:该Id的FSMController不存在或已被销毁!!!");
|
2025-04-10 11:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|