修改fsm
This commit is contained in:
53
Assets/03.FiniteStateMachine/RunTime/Use/FSMExtension.cs
Normal file
53
Assets/03.FiniteStateMachine/RunTime/Use/FSMExtension.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
|
||||
namespace Stary.Evo.FiniteStateMachine
|
||||
{
|
||||
public static class FSMInitialize
|
||||
{
|
||||
private static Dictionary<string, FSMController> _fsmControllers =
|
||||
new Dictionary<string, FSMController>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建FSMController
|
||||
/// </summary>
|
||||
/// <returns>返回创建的FSMController的识别Int值</returns>
|
||||
public static void CreateFSMController<T>(Transform parent) where T : FSMController
|
||||
{
|
||||
if (_fsmControllers.ContainsKey(typeof(T).Name))
|
||||
{
|
||||
Debug.LogError($"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 : FSMController
|
||||
{
|
||||
if (_fsmControllers.TryGetValue(typeof(T).Name, out var controller))
|
||||
{
|
||||
GameObject.Destroy(controller.gameObject);
|
||||
_fsmControllers.Remove(typeof(T).Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("该Id的FSMController不存在或已被销毁!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user