This commit is contained in:
CortexCore
2024-11-03 16:38:17 +08:00
parent 056e2cada5
commit 4ba741408d
4693 changed files with 2445 additions and 5443 deletions

View File

@@ -8,43 +8,70 @@ using UnityEngine.UIElements;
namespace BITKit.UX
{
[AttributeUsage(AttributeTargets.Field|AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class UXBindPathAttribute : Attribute
{
public string Path;
public bool CanBeNull;
public UXBindPathAttribute(){}
public UXBindPathAttribute()
{
}
public UXBindPathAttribute(string path)
{
Path = path;
}
public UXBindPathAttribute(string path,bool canBeNull)
public UXBindPathAttribute(string path, bool canBeNull)
{
Path = path;
CanBeNull = canBeNull;
}
}
public class UXUtils
public class UXUtils
{
public static void Inject(object self)
public static void Inject(object self,VisualElement root=null)
{
UIDocument document;
UIDocument document = null;
var field = self.GetType().GetField("document",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var rootVisualElementFieldInfo = self.GetType().GetField("RootVisualElement",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var rootVisualElementPropertyInfo = self.GetType().GetProperty("RootVisualElement",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
switch (field)
var rootVisualElement = (rootVisualElementFieldInfo, rootVisualElementPropertyInfo) switch
{
case null when self is MonoBehaviour monoBehaviour && monoBehaviour.TryGetComponent<UIDocument>(out document):
break;
case not null when field.GetValue(self) is UIDocument _document:
document = _document;
break;
default:
BIT4Log.Warning<UXUtils>($"document 未赋值或未找到");
return;
{ } when rootVisualElementPropertyInfo?.GetValue(self) is VisualElement ve => ve,
{ } when rootVisualElementFieldInfo?.GetValue(self) is VisualElement ve => ve,
_ => root,
};
if (rootVisualElement is not null)
{
}
else
{
switch (field)
{
case null when self is MonoBehaviour monoBehaviour &&
monoBehaviour.TryGetComponent<UIDocument>(out document):
break;
case not null when field.GetValue(self) is UIDocument _document:
document = _document;
break;
default:
BIT4Log.Warning<UXUtils>($"document 未赋值或未找到@{self.GetType().Name}");
return;
}
}
foreach (var fieldInfo in self.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
@@ -52,7 +79,12 @@ namespace BITKit.UX
)
{
var bindPathAtt = fieldInfo.GetCustomAttribute<UXBindPathAttribute>();
VisualElement ve = document.rootVisualElement;
VisualElement ve = (document, rootVisualElement) switch
{
(_, not null) => rootVisualElement,
(not null, _) => document.rootVisualElement,
_ => throw new NotImplementedException(),
};
foreach (var path in bindPathAtt.Path.Split("."))
{
ve = ve.Q(path);
@@ -71,21 +103,6 @@ namespace BITKit.UX
BIT4Log.Warning<UXUtils>(field!.Name);
}
}
// 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;
// }
// }
}
}
}