这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

38 行
1.1 KiB

using System;
using UnityEngine;
public static class ScriptableObjectHelper
{
public static void GenerateButtonsForEvents<T>(UnityEngine.Object target)
where T : ScriptableObject
{
var targetIr = target as T;
if (targetIr != null)
{
var typeIr = targetIr.GetType();
var events = typeIr.GetEvents();
foreach (var ev in events)
{
if (GUILayout.Button(ev.Name))
{
//Delegates doesn't support direct access to RaiseMethod, must use backing field
// https://stackoverflow.com/questions/14885325/eventinfo-getraisemethod-always-null
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/44b0d573-5c53-47b0-8e85-6056cbae95b0/raising-an-event-via-reflection
var eventDelagate = typeIr.GetField(ev.Name, System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic)
?.GetValue(targetIr) as MulticastDelegate;
try
{
eventDelagate?.DynamicInvoke();
}
catch
{
Debug.LogWarning($"Event '{ev.Name}' requires some arguments which weren't provided. Delegate cannot be invoked directly from UI.");
}
}
}
}
}
}