解決配置表中配置了一些方法料祠,例如新手引導(dǎo)骆捧,劇情等系統(tǒng),需要配置一些執(zhí)行的方法字符串髓绽,然后利用反射運(yùn)行方法敛苇。
using System;
using System.Reflection;
using UnityEngine;
public class SelectAction
{
public object OnSelect(object[] value)
{
Debug.LogError("OnSelect:" + value[0]);
Action<string> action = value[1] as Action<string>;
action.Invoke(value[0].ToString());
return value;
}
}
public class ReflectionMethod : MonoBehaviour
{
public void Awake()
{
Action<string> action = OnSelectFinish;
// 支持多參數(shù)
ExcuteMethod(new SelectAction(), "OnSelect", "啊哈哈哈哈哈哈", action);
}
private void ExcuteMethod(object obj, string methodName, params object[] parameterValues)
{
MethodInfo method = obj.GetType().GetMethod(methodName.Trim());
if (method == null)
{
Debug.LogError("方法不存在!");
}
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length > 0)
{
object[] pars = new object[] { parameterValues };
var info = (method.Invoke(obj, pars)) as object[];
Debug.LogError("回調(diào):" + info[0]);
}
else
{
Debug.LogError("沒(méi)有方法!");
}
}
private void OnSelectFinish(string info)
{
Debug.LogError("OnSelectFinish:" + info);
}
}