using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using Newtonsoft.Json; #if UNITY_EDITOR using UnityEditor.UIElements; using Cysharp.Threading.Tasks; using UnityEditor; #endif using UnityEngine; using UnityEngine.UIElements; using Object = UnityEngine.Object; namespace BITKit.Events { [Serializable] public class UnityEventData { public Object Target; public string MethodName; } [Serializable] public class UnityEvent { public List Targets = new List(); public void Invoke(params object[] 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(string.Join(",",method?.GetParameters().Select(x=>x.Name))); throw; } } } } #if UNITY_EDITOR [CustomPropertyDrawer(typeof(UnityEvent))] public class UnityEventDataPropertyDrawer:PropertyDrawer { private VisualElement root; private UnityEvent agent; private SerializedProperty _property; private static readonly List _unityMethodNames = typeof(MonoBehaviour).GetMethods().Select(x => x.Name).ToList(); public override VisualElement CreatePropertyGUI(SerializedProperty property) { _property = property; agent = property.Get(); root = new VisualElement { style = { paddingLeft = 10, paddingTop = 10, paddingRight = 10, paddingBottom = 10, } }; root.styleSheets.Add(BITEditorUtils.InspectorStyleSheet); Update(); return root; } private void Update() { root.Clear(); var targets = _property.FindPropertyRelative(nameof(UnityEvent.Targets)); _property.serializedObject.Update(); var label = root.Create