51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Opsive.BehaviorDesigner.Runtime.Tasks;
|
|
using Opsive.GraphDesigner.Runtime;
|
|
using Opsive.GraphDesigner.Runtime.Variables;
|
|
using Opsive.Shared.Utility;
|
|
using SLSUtilities.FunctionalAnimation;
|
|
using UnityEngine;
|
|
|
|
namespace Cielonos.MainGame.Characters.AI
|
|
{
|
|
[Description("调用自定义函数。")]
|
|
[NodeIcon("Assets/Sprites/Icon/Play.png")]
|
|
[Category("Cielonos")]
|
|
public class InvokeCustomFunction : AutomataActionBase
|
|
{
|
|
[Tooltip("要调用的函数名,是当前角色类中的一个方法,可以是私有的。")]
|
|
public string functionName;
|
|
|
|
[Tooltip("函数参数列表,按顺序填写。")]
|
|
public List<SharedVariable<object>> parameters = new List<SharedVariable<object>>();
|
|
|
|
public override TaskStatus OnUpdate()
|
|
{
|
|
//使用反射调用函数
|
|
var method = self.GetType().GetMethod(functionName,
|
|
System.Reflection.BindingFlags.Instance |
|
|
System.Reflection.BindingFlags.Public |
|
|
System.Reflection.BindingFlags.NonPublic);
|
|
if (method == null)
|
|
{
|
|
throw new Exception($"函数 {functionName} 在 {self.GetType().Name} 中未找到");
|
|
}
|
|
|
|
//根据函数的参数数量调用函数
|
|
var parameters = method.GetParameters();
|
|
var args = new object[parameters.Length];
|
|
if (args.Length != this.parameters.Count)
|
|
{
|
|
throw new Exception("参数数量不匹配");
|
|
}
|
|
for (int i = 0; i < parameters.Length; i++)
|
|
{
|
|
args[i] = this.parameters[i].Value;
|
|
}
|
|
|
|
method.Invoke(self, args);
|
|
return TaskStatus.Success;
|
|
}
|
|
}
|
|
} |