using System; using System.Collections.Generic; using UnityEngine; namespace QFramework { internal class ScriptAction : IAction { public System.Action OnFinished { get; set; } private ScriptAction() { } private static readonly SimpleObjectPool mPool = new SimpleObjectPool(() => new ScriptAction(), null, 10); public string prefabPath; string finishedEvent; string name; public static ScriptAction Allocate(string name, string prefabPath, string finishedEvent, System.Action OnFinished = null) { var retNode = mPool.Allocate(); retNode.ActionID = ActionKit.ID_GENERATOR++; retNode.Deinited = false; retNode.Reset(); retNode.OnFinished = OnFinished; retNode.finishedEvent = finishedEvent; retNode.prefabPath = prefabPath; retNode.name = name; return retNode; } public ulong ActionID { get; set; } public ActionStatus Status { get; set; } public void OnStart() { StringEventSystem.Global.Register(finishedEvent, OnCustomFinished); GameObject obj = GameObject.Instantiate(Resources.Load($"CustomAction/{prefabPath}").As()); obj.name = name; } private void OnCustomFinished() { StringEventSystem.Global.UnRegister(finishedEvent, OnCustomFinished); this.Finish(); } public void OnExecute(float dt) { } public void OnFinish() { } public void Reset() { Status = ActionStatus.NotStart; Paused = false; } public bool Paused { get; set; } public void Deinit() { if (!Deinited) { OnFinished = null; Deinited = true; mPool.Recycle(this); } } public bool Deinited { get; set; } } }