1
This commit is contained in:
@@ -2,8 +2,10 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor.UIElements;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor.UIElements;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEditor;
|
||||
#endif
|
||||
@@ -26,10 +28,30 @@ namespace BITKit.Events
|
||||
|
||||
public void Invoke(params object[] objects)
|
||||
{
|
||||
foreach (var x in Targets)
|
||||
{
|
||||
x.Target.GetType().GetMethod(x.MethodName)?.Invoke(x.Target,objects);
|
||||
}
|
||||
foreach (var x in Targets)
|
||||
{
|
||||
var methodName = x.MethodName;
|
||||
var target = x.Target;
|
||||
var split = methodName.Split('/');
|
||||
if (x.Target is GameObject gameObject&& split.Length is 2 && BITSharp.TryGetTypeFromFullName(split[0],out var type) && gameObject.TryGetComponent(type,out var _target))
|
||||
{
|
||||
target = _target;
|
||||
methodName = split[1];
|
||||
}
|
||||
|
||||
|
||||
var method =target.GetType().GetMethod(methodName);
|
||||
try
|
||||
{
|
||||
method?.Invoke(target, objects);
|
||||
}
|
||||
catch (TargetParameterCountException e)
|
||||
{
|
||||
BIT4Log.Log<UnityEvent>(string.Join(",",method?.GetParameters().Select(x=>x.Name)));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
@@ -42,6 +64,7 @@ namespace BITKit.Events
|
||||
|
||||
private SerializedProperty _property;
|
||||
|
||||
private static readonly List<string> _unityMethodNames = typeof(MonoBehaviour).GetMethods().Select(x => x.Name).ToList();
|
||||
public override VisualElement CreatePropertyGUI(SerializedProperty property)
|
||||
{
|
||||
_property = property;
|
||||
@@ -101,15 +124,55 @@ namespace BITKit.Events
|
||||
|
||||
if (x.Target is not null)
|
||||
{
|
||||
field.choices = x.Target.GetType().GetMethods().Select(x => x.Name).ToList();
|
||||
switch (x.Target)
|
||||
{
|
||||
case GameObject gameObject:
|
||||
var methodNames = new List<string>();
|
||||
foreach (var mono in gameObject.GetComponents<MonoBehaviour>())
|
||||
{
|
||||
foreach (var methodInfo in mono.GetComponents<MonoBehaviour>().Cast<object>().Append(gameObject)
|
||||
.SelectMany(_mono => _mono
|
||||
.GetType()
|
||||
.GetMethods(BindingFlags.Instance | BindingFlags.Public |
|
||||
BindingFlags.NonPublic))
|
||||
.Where(info =>info.IsSpecialName is false && info.GetCustomAttribute<ObsoleteAttribute>() is null)
|
||||
)
|
||||
{
|
||||
methodNames.Add($"{mono.GetType().FullName}/{methodInfo.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var VARIABLE in gameObject
|
||||
.GetType()
|
||||
.GetMethods()
|
||||
.Where(info =>info.IsSpecialName is false && info.GetCustomAttribute<ObsoleteAttribute>() is null)
|
||||
)
|
||||
{
|
||||
methodNames.Add(VARIABLE.Name);
|
||||
}
|
||||
field.choices = methodNames;
|
||||
break;
|
||||
default:
|
||||
field.choices = x.Target
|
||||
.GetType()
|
||||
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Where(x=>x.IsSpecialName is false)
|
||||
.Where(x=>x.GetCustomAttribute<ObsoleteAttribute>() is null)
|
||||
.Select(x => $"{x.Name}")
|
||||
//.Where(x=>_unityMethodNames.Contains(x) is false)
|
||||
.ToList();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
field.value = x.MethodName;
|
||||
|
||||
field.RegisterValueChangedCallback(changeEvent =>
|
||||
{
|
||||
x.MethodName = changeEvent.newValue;
|
||||
EditorUtility.SetDirty(_property.serializedObject.targetObject);
|
||||
});
|
||||
|
||||
field.value = x.MethodName;
|
||||
|
||||
field.style.flexGrow = 1;
|
||||
|
||||
|
Reference in New Issue
Block a user