新增自定义脚本的Action

This commit is contained in:
shenjianxing 2024-12-20 17:05:19 +08:00
parent 5949ba5d04
commit 8e7fc41d4d
6 changed files with 129 additions and 0 deletions

View File

@ -191,6 +191,11 @@ public class ActionHelper
var strAction = (XMLTool.StringListAction)act;
return LedAction.Allocate(act.Value, strAction.args[0], strAction.args[1]);
}
case "Script":
{
var strAction = (XMLTool.StringListAction)act;
return ScriptAction.Allocate(act.Name, act.Value, strAction.args[0]);
}
default:
Debug.LogError($"没有找到此Action的类型{act.Type}");
break;

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
namespace QFramework
{
internal class ScriptAction : IAction
{
public System.Action OnFinished { get; set; }
private ScriptAction()
{
}
private static readonly SimpleObjectPool<ScriptAction> mPool =
new SimpleObjectPool<ScriptAction>(() => 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<GameObject>());
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; }
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6958205303fbb7541b8272f244747fc0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6d7f9813c337725468c8739dba68c439
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -869,10 +869,26 @@ namespace XMLTool
newAction = act;
}
break;
case "Script":
{
var act = new StringListAction();
XAttribute finishedEvent = action.Attribute("finishedEvent");
if (finishedEvent != null)
{
act.args.Add(finishedEvent.Value);
}
else
{
act.args.Add("");
}
newAction = act;
}
break;
default:
newAction = new Action();
break;
}
newAction.Type = action.Attribute("type")?.Value;
newAction.Name = action.Attribute("name")?.Value;
newAction.Value = action.Attribute("value")?.Value;

View File

@ -85,6 +85,9 @@
number 是数值 支持 小数点和横线 例如 12.34 3-5 -->
<Action type="Led" value="Number" number="12.34" color="255,0,0,255"></Action>
<!--给程序使用的:自定义脚本 挂空预制体上 预制体放在Resources/CustomAction目录下 执行完毕后记得使用StringEventSystem.Global.Send(finishedEvent)-->
<Action type="Script" value="MyAction" finishedEvent="111"/>
<!--预加载模块 要在app.xml的Data标签内-->
<PreLoad>
<Action type="Parallel">