55 lines
1.4 KiB
C#
55 lines
1.4 KiB
C#
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<UXUtils>($"{self.GetType().Name} not find {nameof(UIDocument)}");
|
|
return;
|
|
}
|
|
if (field.GetValue(self) is not UIDocument document)
|
|
{
|
|
if (self is MonoBehaviour monoBehaviour && (document = monoBehaviour.GetComponentInParent<UIDocument>()))
|
|
{
|
|
field.SetValue(self,document);
|
|
}
|
|
else
|
|
{
|
|
BIT4Log.Warning<UXUtils>($"document 未赋值");
|
|
return;
|
|
}
|
|
}
|
|
foreach (var fieldInfo in self.GetType()
|
|
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
|
.Where(x=>x.GetCustomAttribute<UXBindPathAttribute>() is not null)
|
|
)
|
|
{
|
|
var bindPathAtt = fieldInfo.GetCustomAttribute<UXBindPathAttribute>();
|
|
var ve = document.rootVisualElement.Q(bindPathAtt.Path);
|
|
fieldInfo.SetValue(self,ve);
|
|
}
|
|
}
|
|
}
|
|
}
|