using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEngine; using UnityEngine.UIElements; namespace BITKit.UX { public class UXBindPathAttribute : Attribute { public string Path; public UXBindPathAttribute(){} public UXBindPathAttribute(string path) { Path = path; } } public class UXUtils { public static void Inject(object self) { var field = self.GetType().GetField("document", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (field is null) { BIT4Log.Warning($"{self.GetType().Name} not find {nameof(UIDocument)}"); return; } if (field.GetValue(self) is not UIDocument document) { if (self is MonoBehaviour monoBehaviour && (document = monoBehaviour.GetComponentInParent())) { field.SetValue(self,document); } else { BIT4Log.Warning($"document 未赋值"); return; } } foreach (var fieldInfo in self.GetType() .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Where(x=>x.GetCustomAttribute() is not null) ) { var bindPathAtt = fieldInfo.GetCustomAttribute(); var ve = document.rootVisualElement.Q(bindPathAtt.Path); fieldInfo.SetValue(self,ve); } } } }