1
This commit is contained in:
@@ -11,44 +11,66 @@ namespace BITKit.UX
|
||||
public class UXBindPathAttribute : Attribute
|
||||
{
|
||||
public string Path;
|
||||
public bool CanBeNull;
|
||||
public UXBindPathAttribute(){}
|
||||
public UXBindPathAttribute(string path)
|
||||
{
|
||||
Path = path;
|
||||
}
|
||||
public UXBindPathAttribute(string path,bool canBeNull)
|
||||
{
|
||||
Path = path;
|
||||
CanBeNull = canBeNull;
|
||||
}
|
||||
}
|
||||
public class UXUtils
|
||||
{
|
||||
public static void Inject(object self)
|
||||
{
|
||||
UIDocument document;
|
||||
|
||||
var field = self.GetType().GetField("document",
|
||||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
if (field is null)
|
||||
|
||||
|
||||
switch (field)
|
||||
{
|
||||
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 未赋值");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if(bindPathAtt.CanBeNull is false && ve is null)
|
||||
BIT4Log.LogException(new NullReferenceException($"未找到{bindPathAtt.Path}"));
|
||||
fieldInfo.SetValue(self,ve);
|
||||
}
|
||||
|
||||
|
||||
// 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;
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user