53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
|
||
|
||
namespace Stary.Evo.FiniteStateMachine
|
||
{
|
||
public static class FSMExtension
|
||
{
|
||
private static Dictionary<string, AbstractFSMController> _fsmControllers =
|
||
new Dictionary<string, AbstractFSMController>();
|
||
|
||
|
||
/// <summary>
|
||
/// 创建FSMController
|
||
/// </summary>
|
||
/// <returns>返回创建的FSMController的识别Int值</returns>
|
||
public static void CreateFSMController<T>(Transform parent) where T : AbstractFSMController
|
||
{
|
||
if (_fsmControllers.ContainsKey(typeof(T).Name))
|
||
{
|
||
Debug.LogError($"UnityEvo:FSM控制器已存在!!!_key:{typeof(T).Name}");
|
||
}
|
||
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>
|
||
public static void DestroyFSMController<T>() where T : AbstractFSMController
|
||
{
|
||
if (_fsmControllers.TryGetValue(typeof(T).Name, out var controller))
|
||
{
|
||
GameObject.Destroy(controller.gameObject);
|
||
_fsmControllers.Remove(typeof(T).Name);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("UnityEvo:该Id的FSMController不存在或已被销毁!!!");
|
||
}
|
||
}
|
||
}
|
||
} |