diff --git a/Assets/03.FiniteStateMachine/README.md b/Assets/03.FiniteStateMachine/README.md index 39fa340..a12604e 100644 --- a/Assets/03.FiniteStateMachine/README.md +++ b/Assets/03.FiniteStateMachine/README.md @@ -1 +1 @@ -# 代码检查工具 +# 状态机工具 diff --git a/Assets/03.FiniteStateMachine/RunTime/Abstract.meta b/Assets/03.FiniteStateMachine/RunTime/Abstract.meta new file mode 100644 index 0000000..f8bb9ab --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/Abstract.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a4c1b2e6dc7695646be9fd2a98863529 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/03.FiniteStateMachine/RunTime/Abstract/IState.cs b/Assets/03.FiniteStateMachine/RunTime/Abstract/IState.cs new file mode 100644 index 0000000..b5ce039 --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/Abstract/IState.cs @@ -0,0 +1,87 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Stary.Evo.FiniteStateMachine +{ + public interface IState + { + public FSMController Machine { get; set; } + public void OnEnter(); + public void OnExit(); + public void Update(); + public void FixedUpdate(); + public void OnInit(); + public void OnDestory(); + + } + + public abstract class AbstractState : IState + { + public FSMController Machine { get; set; } + public abstract void OnEnter(); + public abstract void OnExit(); + public abstract void Update(); + public abstract void FixedUpdate(); + public abstract void OnInit(); + public abstract void OnDestory(); + } + + /// + /// 状态中具体行为: + /// (OnInitAction:初始化) + /// (OnEnterAction:进入) + /// (UpdateAction:循环) + /// (FixedUpdateAction:后循环) + /// (OnExitAction:离开) + /// (OnDestoryAction:销毁) + /// + public struct StateDateAction + { + public System.Action OnEnterAction; + public System.Action OnExitAction; + public System.Action UpdateAction; + public System.Action FixedUpdateAction; + public System.Action OnInitAction; + public System.Action OnDestoryAction; + } + + /// + /// 单个状态 + /// + public class SingleState : AbstractState + { + public StateDateAction stateDate; + public bool IsRunning; + + public override void FixedUpdate() + { + stateDate.FixedUpdateAction?.Invoke(); + } + + public override void OnDestory() + { + stateDate.OnDestoryAction?.Invoke(); + } + + public override void OnEnter() + { + stateDate.OnEnterAction?.Invoke(); + } + + public override void OnExit() + { + stateDate.OnExitAction?.Invoke(); + } + + public override void OnInit() + { + stateDate.OnInitAction?.Invoke(); + } + + public override void Update() + { + stateDate.UpdateAction?.Invoke(); + } + } +} diff --git a/Assets/03.FiniteStateMachine/RunTime/IState.cs.meta b/Assets/03.FiniteStateMachine/RunTime/Abstract/IState.cs.meta similarity index 100% rename from Assets/03.FiniteStateMachine/RunTime/IState.cs.meta rename to Assets/03.FiniteStateMachine/RunTime/Abstract/IState.cs.meta diff --git a/Assets/03.FiniteStateMachine/RunTime/Base.meta b/Assets/03.FiniteStateMachine/RunTime/Base.meta new file mode 100644 index 0000000..78ab703 --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/Base.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 482775b04d27d6644bc8d880e135e6ac +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/03.FiniteStateMachine/RunTime/Base/FSMController.cs b/Assets/03.FiniteStateMachine/RunTime/Base/FSMController.cs new file mode 100644 index 0000000..a0d36f9 --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/Base/FSMController.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace Stary.Evo.FiniteStateMachine +{ + public class FSMController : MonoBehaviour + { + private Dictionary states = new Dictionary(); + + public void AddState(SingleState state) + { + var type = state.GetType(); + if (!states.ContainsKey(type)) + { + // 添加时初始化 + state.Machine = this; + state.OnInit(); + states.Add(type, state); + } + else + { + Debug.LogError("该FSMController中已经添加过该状态!!!"); + } + } + + /* //public bool IsRunning() where T : SingleState => IsRunning(typeof(T)); + + //public void Enter() where T : SingleState => Enter(typeof(T)); + + //public void Exit() where T : SingleState => Exit(typeof(T)); + + //public void ForceEnter() where T : SingleState => ForceEnter(typeof(T));*/ + + public bool IsRunning(Type type) + { + if (states.ContainsKey(type)) + { + if (states[type].IsRunning) return true; + } + else + { + Debug.LogError("状态已不存在!!!"); + } + return false; + } + + public void Enter(Type type) + { + if (states.ContainsKey(type)) + { + if (IsRunning(type)) + { + Debug.LogError("该状态正在进行,无法再次进入!!!"); + return; + } + ForceEnter(type); + } + else + { + Debug.LogError("状态已不存在!!!"); + } + + } + + public void ForceEnter(Type type) + { + if (states.ContainsKey(type)) + { + SingleState state = states[type]; + state.OnEnter(); + state.IsRunning = true; + } + else + { + Debug.LogError("状态已不存在!!!"); + } + } + + public void Exit(Type type) + { + if (states.TryGetValue(type, out SingleState state)) + { + if (!IsRunning(type)) return; + state.IsRunning = false; + state.OnExit(); + } + else + { + Debug.LogError("状态已不存在!!!"); + } + + } + + public void ExitAll() + { + foreach (var state in states) + { + if (state.Value.IsRunning) Exit(state.Key); + } + } + + private void Update() + { + foreach (var state in states) + { + if (state.Value.IsRunning) state.Value.Update(); + } + } + + private void FixedUpdate() + { + foreach (var state in states) + { + if (state.Value.IsRunning) state.Value.FixedUpdate(); + } + } + + private void OnDestroy() + { + ExitAll(); + // 遍历字典中的每个状态 + foreach (var key in states.Keys.ToList()) + { + states[key].OnDestory(); + // 将每个值设置为 null,释放引用 + states[key] = null; + } + // 清空字典 + states.Clear(); + } + } +} \ No newline at end of file diff --git a/Assets/03.FiniteStateMachine/RunTime/FSMController.cs.meta b/Assets/03.FiniteStateMachine/RunTime/Base/FSMController.cs.meta similarity index 100% rename from Assets/03.FiniteStateMachine/RunTime/FSMController.cs.meta rename to Assets/03.FiniteStateMachine/RunTime/Base/FSMController.cs.meta diff --git a/Assets/03.FiniteStateMachine/RunTime/FSMController.cs b/Assets/03.FiniteStateMachine/RunTime/FSMController.cs deleted file mode 100644 index 9848406..0000000 --- a/Assets/03.FiniteStateMachine/RunTime/FSMController.cs +++ /dev/null @@ -1,132 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using UnityEngine; - -public class FSMController : MonoBehaviour -{ - private Dictionary states = new Dictionary(); - - public void AddState(SingleState state) - { - var type = state.GetType(); - if (!states.ContainsKey(type)) - { - // ʱʼ - state.Machine = this; - state.OnInit(); - states.Add(type, state); - } - else - { - Debug.LogError("FSMControllerѾӹ״̬"); - } - } - -/* //public bool IsRunning() where T : SingleState => IsRunning(typeof(T)); - - //public void Enter() where T : SingleState => Enter(typeof(T)); - - //public void Exit() where T : SingleState => Exit(typeof(T)); - - //public void ForceEnter() where T : SingleState => ForceEnter(typeof(T));*/ - - public bool IsRunning(Type type) - { - if (states.ContainsKey(type)) - { - if (states[type].IsRunning) return true; - } - else - { - Debug.LogError("״̬Ѳڣ"); - } - return false; - } - - public void Enter(Type type) - { - if (states.ContainsKey(type)) - { - if (IsRunning(type)) - { - Debug.LogError("״̬ڽУ޷ٴν룡"); - return; - } - ForceEnter(type); - } - else - { - Debug.LogError("״̬Ѳڣ"); - } - - } - - public void ForceEnter(Type type) - { - if (states.ContainsKey(type)) - { - SingleState state = states[type]; - state.OnEnter(); - state.IsRunning = true; - } - else - { - Debug.LogError("״̬Ѳڣ"); - } - } - - public void Exit(Type type) - { - if (states.TryGetValue(type, out SingleState state)) - { - if (!IsRunning(type)) return; - state.IsRunning = false; - state.OnExit(); - } - else - { - Debug.LogError("״̬Ѳڣ"); - } - - } - - public void ExitAll() - { - foreach (var state in states) - { - if (state.Value.IsRunning) Exit(state.Key); - } - } - - private void Update() - { - foreach (var state in states) - { - if (state.Value.IsRunning) state.Value.Update(); - } - } - - private void FixedUpdate() - { - foreach (var state in states) - { - if (state.Value.IsRunning) state.Value.FixedUpdate(); - } - } - - private void OnDestroy() - { - ExitAll(); - // ֵеÿ״̬ - foreach (var key in states.Keys.ToList()) - { - states[key].OnDestory(); - // ÿֵΪ nullͷ - states[key] = null; - } - // ֵ - states.Clear(); - } -} \ No newline at end of file diff --git a/Assets/03.FiniteStateMachine/RunTime/FSMInitialize.cs b/Assets/03.FiniteStateMachine/RunTime/FSMInitialize.cs deleted file mode 100644 index 60d4635..0000000 --- a/Assets/03.FiniteStateMachine/RunTime/FSMInitialize.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using UnityEngine; -using UnityEngine.SceneManagement; - -public static class FSMInitialize -{ - /// - /// ״̬ʼ - /// - /// ָ - /// - public static FSMManager Init(Scene targetScene) - { - GameObject targetObject = targetScene.GetRootGameObjects().FirstOrDefault(go => go.name == "FSMManger"); - FSMManager fSMManager; - - if (targetObject == null) - { - targetObject = new GameObject("FSMManger"); - SceneManager.MoveGameObjectToScene(targetObject, targetScene); - - fSMManager = new FSMManager(); - fSMManager.FSMMangerObject = targetObject; - - void OnSceneUnloaded(Scene scene) - { - // жʱͷŻ - if (scene.name == targetScene.name) - { - GameObject.Destroy(targetObject); - fSMManager = null; - } - } - SceneManager.sceneUnloaded += OnSceneUnloaded; - - return fSMManager; - } - else - { - Debug.LogError(": "+targetScene.name + "ѾʼFSM״̬"); - return null; - } - } -} diff --git a/Assets/03.FiniteStateMachine/RunTime/FSMManager.cs b/Assets/03.FiniteStateMachine/RunTime/FSMManager.cs deleted file mode 100644 index dd62904..0000000 --- a/Assets/03.FiniteStateMachine/RunTime/FSMManager.cs +++ /dev/null @@ -1,146 +0,0 @@ -using System; -using System.Collections.Generic; -using UnityEngine; - -public class FSMManager -{ - public GameObject FSMMangerObject; - private Dictionary controllers = new Dictionary(); - private Dictionary states = new Dictionary(); - private int nextControllerId = 0; - private int nextStateId = 0; - - /// - /// FSMController - /// - /// شFSMControllerʶIntֵ - public int CreateFSMController() - { - if(FSMMangerObject == null) - { - Debug.LogError("FSM״̬δʼ"); - return -1; - } - else - { - int id = nextControllerId++; - GameObject newController = new GameObject("FSMController" + id); - newController.transform.SetParent(FSMMangerObject.transform); - var controller = newController.AddComponent(); - controllers[id] = controller; - return id; - } - } - - /// - /// ָFSMController - /// - /// - public void DestroyFSMController(int controllerId) - { - if (controllers.TryGetValue(controllerId, out var controller)) - { - GameObject.Destroy(controller.gameObject); - controllers.Remove(controllerId); - } - else - { - Debug.LogError("IdFSMControllerڻѱ٣"); - } - } - - /// - /// ˳ָControllerе״̬ - /// - /// - public void ExitAllStateInController(int controllerId) - { - if (controllers.TryGetValue(controllerId, out var controller)) - { - controller.ExitAll(); - } - else - { - Debug.LogError("IdFSMControllerڻѱ٣"); - } - } - - /// - /// ״̬ - /// - /// ״̬صControllerId - /// ״̬Ϊ - /// - public int CreateState(int controllerId, StateDateAction stateDateAction) - { - if (controllers.TryGetValue(controllerId, out var controller)) - { - // stateӵָcontroller - SingleState state = new SingleState(); - state.stateDate = stateDateAction; - controller.AddState(state); - int id = nextStateId++; - states[id] = state; - return id; - - } - else - { - Debug.LogError("IdFSMControllerڻѱ٣״̬ʼʧܣ"); - return -1; - } - } - - /// - /// ָ״̬ - /// - /// - public void EnterState(int stateId) - { - if(states.TryGetValue(stateId, out var state)) - { - state.Machine.Enter(state.GetType()); - } - else - { - Debug.LogError("Id״̬ڣ"); - } - } - - /// - /// 뿪ָ״̬ - /// - /// - public void ExitState(int stateId) - { - if (states.TryGetValue(stateId, out var state)) - { - state.Machine.Exit(state.GetType()); - } - else - { - Debug.LogError("Id״̬ڣ"); - } - } - - /// - /// ָ״̬ - /// - /// - public void DestroyState(int stateId) - { - if (states.TryGetValue(stateId, out var state)) - { - states.Remove(stateId); - } - } - - // ״̬ӵFSMController - public void AddStateToController(int controllerId, int stateId) - { - if (controllers.TryGetValue(controllerId, out var controller) && states.TryGetValue(stateId, out var state)) - { - controller.AddState(state); - } - } -} \ No newline at end of file diff --git a/Assets/03.FiniteStateMachine/RunTime/FiniteStateMachine.RunTime.asmdef b/Assets/03.FiniteStateMachine/RunTime/FiniteStateMachine.RunTime.asmdef new file mode 100644 index 0000000..76d724d --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/FiniteStateMachine.RunTime.asmdef @@ -0,0 +1,3 @@ +{ + "name": "FiniteStateMachine.RunTime" +} diff --git a/Assets/03.FiniteStateMachine/RunTime/FiniteStateMachine.RunTime.asmdef.meta b/Assets/03.FiniteStateMachine/RunTime/FiniteStateMachine.RunTime.asmdef.meta new file mode 100644 index 0000000..5fc3e29 --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/FiniteStateMachine.RunTime.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: fd2529d4be47388488cf47fbdd0ee1bf +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/03.FiniteStateMachine/RunTime/IState.cs b/Assets/03.FiniteStateMachine/RunTime/IState.cs deleted file mode 100644 index 72d43d7..0000000 --- a/Assets/03.FiniteStateMachine/RunTime/IState.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public interface IState -{ - public FSMController Machine { get; set; } - public void OnEnter(); - public void OnExit(); - public void Update(); - public void FixedUpdate(); - public void OnInit(); - public void OnDestory(); - -} - -public abstract class AbstractState : IState -{ - public FSMController Machine { get; set; } - public abstract void OnEnter(); - public abstract void OnExit(); - public abstract void Update(); - public abstract void FixedUpdate(); - public abstract void OnInit(); - public abstract void OnDestory(); -} - -/// -/// ״̬оΪ -/// OnInitActionʼ -/// OnEnterAction룩 -/// UpdateActionѭ -/// FixedUpdateActionѭ -/// OnExitAction뿪 -/// OnDestoryAction٣ -/// -public struct StateDateAction -{ - public System.Action OnEnterAction; - public System.Action OnExitAction; - public System.Action UpdateAction; - public System.Action FixedUpdateAction; - public System.Action OnInitAction; - public System.Action OnDestoryAction; -} - -/// -/// ״̬ -/// -public class SingleState : AbstractState -{ - public StateDateAction stateDate; - public bool IsRunning; - - public override void FixedUpdate() - { - stateDate.FixedUpdateAction?.Invoke(); - } - - public override void OnDestory() - { - stateDate.OnDestoryAction?.Invoke(); - } - - public override void OnEnter() - { - stateDate.OnEnterAction?.Invoke(); - } - - public override void OnExit() - { - stateDate.OnExitAction?.Invoke(); - } - - public override void OnInit() - { - stateDate.OnInitAction?.Invoke(); - } - - public override void Update() - { - stateDate.UpdateAction?.Invoke(); - } -} diff --git a/Assets/03.FiniteStateMachine/RunTime/PlayerFSMExample.cs b/Assets/03.FiniteStateMachine/RunTime/PlayerFSMExample.cs deleted file mode 100644 index b6228ca..0000000 --- a/Assets/03.FiniteStateMachine/RunTime/PlayerFSMExample.cs +++ /dev/null @@ -1,53 +0,0 @@ -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() - { - - } -} \ No newline at end of file diff --git a/Assets/03.FiniteStateMachine/RunTime/PlayerFSMExample.cs.meta b/Assets/03.FiniteStateMachine/RunTime/PlayerFSMExample.cs.meta deleted file mode 100644 index 26a4701..0000000 --- a/Assets/03.FiniteStateMachine/RunTime/PlayerFSMExample.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0deab708fd53bb4428ef48220df57be7 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/03.FiniteStateMachine/RunTime/Use.meta b/Assets/03.FiniteStateMachine/RunTime/Use.meta new file mode 100644 index 0000000..be9e095 --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/Use.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bacf497398db3124994cae839e813842 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/03.FiniteStateMachine/RunTime/Use/FSMInitialize.cs b/Assets/03.FiniteStateMachine/RunTime/Use/FSMInitialize.cs new file mode 100644 index 0000000..992cdc0 --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/Use/FSMInitialize.cs @@ -0,0 +1,50 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using UnityEngine.SceneManagement; + + +namespace Stary.Evo.FiniteStateMachine +{ + public static class FSMInitialize + { + /// + /// 状态机初始化 + /// + /// 指定场景 + /// + public static FSMManager Init(Scene targetScene) + { + GameObject targetObject = targetScene.GetRootGameObjects().FirstOrDefault(go => go.name == "FSMManger"); + FSMManager fSMManager; + + if (targetObject == null) + { + targetObject = new GameObject("FSMManger"); + SceneManager.MoveGameObjectToScene(targetObject, targetScene); + + fSMManager = new FSMManager(); + fSMManager.FSMMangerObject = targetObject; + + void OnSceneUnloaded(Scene scene) + { + // 当场景卸载时释放缓存 + if (scene.name == targetScene.name) + { + GameObject.Destroy(targetObject); + fSMManager = null; + } + } + SceneManager.sceneUnloaded += OnSceneUnloaded; + + return fSMManager; + } + else + { + Debug.LogError("场景: " + targetScene.name + "已经初始化过FSM状态机!!!"); + return null; + } + } + } +} diff --git a/Assets/03.FiniteStateMachine/RunTime/FSMInitialize.cs.meta b/Assets/03.FiniteStateMachine/RunTime/Use/FSMInitialize.cs.meta similarity index 100% rename from Assets/03.FiniteStateMachine/RunTime/FSMInitialize.cs.meta rename to Assets/03.FiniteStateMachine/RunTime/Use/FSMInitialize.cs.meta diff --git a/Assets/03.FiniteStateMachine/RunTime/Use/FSMManager.cs b/Assets/03.FiniteStateMachine/RunTime/Use/FSMManager.cs new file mode 100644 index 0000000..81594ae --- /dev/null +++ b/Assets/03.FiniteStateMachine/RunTime/Use/FSMManager.cs @@ -0,0 +1,150 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + + +namespace Stary.Evo.FiniteStateMachine +{ + public class FSMManager + { + public GameObject FSMMangerObject; + private Dictionary controllers = new Dictionary(); + private Dictionary states = new Dictionary(); + private int nextControllerId = 0; + private int nextStateId = 0; + + /// + /// 创建FSMController + /// + /// 返回创建的FSMController的识别Int值 + public int CreateFSMController() + { + if (FSMMangerObject == null) + { + Debug.LogError("FSM状态机还未初始化!!!"); + return -1; + } + else + { + int id = nextControllerId++; + GameObject newController = new GameObject("FSMController" + id); + newController.transform.SetParent(FSMMangerObject.transform); + var controller = newController.AddComponent(); + controllers[id] = controller; + return id; + } + } + + /// + /// 销毁指定FSMController + /// + /// + public void DestroyFSMController(int controllerId) + { + if (controllers.TryGetValue(controllerId, out var controller)) + { + GameObject.Destroy(controller.gameObject); + controllers.Remove(controllerId); + } + else + { + Debug.LogError("该Id的FSMController不存在或已被销毁!!!"); + } + } + + /// + /// 退出指定Controller中的所有状态 + /// + /// + public void ExitAllStateInController(int controllerId) + { + if (controllers.TryGetValue(controllerId, out var controller)) + { + controller.ExitAll(); + } + else + { + Debug.LogError("该Id的FSMController不存在或已被销毁!!!"); + } + } + + /// + /// 创建状态 + /// + /// 状态挂载的ControllerId + /// 状态行为数据 + /// + public int CreateState(int controllerId, StateDateAction stateDateAction) + { + if (controllers.TryGetValue(controllerId, out var controller)) + { + // 创建state并将其添加到指定controller生命周期中 + SingleState state = new SingleState(); + state.stateDate = stateDateAction; + controller.AddState(state); + int id = nextStateId++; + states[id] = state; + return id; + + } + else + { + Debug.LogError("该Id的FSMController不存在或已被销毁,状态初始化失败!!!"); + return -1; + } + } + + /// + /// 进入指定状态 + /// + /// + public void EnterState(int stateId) + { + if (states.TryGetValue(stateId, out var state)) + { + state.Machine.Enter(state.GetType()); + } + else + { + Debug.LogError("该Id的状态不存在!!!"); + } + } + + /// + /// 离开指定状态 + /// + /// + public void ExitState(int stateId) + { + if (states.TryGetValue(stateId, out var state)) + { + state.Machine.Exit(state.GetType()); + } + else + { + Debug.LogError("该Id的状态不存在!!!"); + } + } + + /// + /// 销毁指定状态 + /// + /// + public void DestroyState(int stateId) + { + if (states.TryGetValue(stateId, out var state)) + { + states.Remove(stateId); + } + } + + // 将状态添加到FSMController + public void AddStateToController(int controllerId, int stateId) + { + if (controllers.TryGetValue(controllerId, out var controller) && states.TryGetValue(stateId, out var state)) + { + controller.AddState(state); + } + } + } +} \ No newline at end of file diff --git a/Assets/03.FiniteStateMachine/RunTime/FSMManager.cs.meta b/Assets/03.FiniteStateMachine/RunTime/Use/FSMManager.cs.meta similarity index 100% rename from Assets/03.FiniteStateMachine/RunTime/FSMManager.cs.meta rename to Assets/03.FiniteStateMachine/RunTime/Use/FSMManager.cs.meta diff --git a/Assets/03.FiniteStateMachine/package.json b/Assets/03.FiniteStateMachine/package.json index 4848191..b86c4b2 100644 --- a/Assets/03.FiniteStateMachine/package.json +++ b/Assets/03.FiniteStateMachine/package.json @@ -1,10 +1,10 @@ { "name": "com.staryevo.finitestatemachine", - "version": "1.0.0", + "version": "1.0.1", "displayName": "03.FiniteStateMachine", - "description": "状态机", + "description": "状态机工具", "unity": "2021.3", - "unityRelease": "30f1", + "unityRelease": "23f1", "keywords": [ "unity", "scirpt"