private void refreshData() { if (mToolbar_Text == null || mToolbar_Text.Length == 0) { mToolbar_Text = I18Ns.Toolbar_title; } _target = (TinaX.XComponent.XComponentScriptBase) this.target; mBaseTypeNames = XComponents.GetTypeNames(); mDict_baseTypeName = new Dictionary <string, int>(); refresh_data = true; }
public static void InjectBindings(XComponentScriptBase component, object injected_obj) { if (component == null || injected_obj == null) { return; } Type type_uobj = typeof(UnityEngine.Object); Type type_go = typeof(UnityEngine.GameObject); Type t_obj = injected_obj.GetType(); foreach (var property in t_obj.GetRuntimeProperties()) { string find_bind_name = property.Name; bool null_able = true; var attr = property.GetCustomAttribute <BindingAttribute>(true); if (attr != null) { null_able = attr.Nullable; if (!string.IsNullOrEmpty(attr.BindingName)) { find_bind_name = attr.BindingName; } } if (property.PropertyType.IsSubclassOf(type_uobj)) { if (component.TryGetBindingUObject(find_bind_name, out var uobj)) { Type __t_uobj = uobj.GetType(); if (__t_uobj.IsSubclassOf(property.PropertyType) || __t_uobj.IsAssignableFrom(property.PropertyType)) { property.SetValue(injected_obj, uobj); continue; } else { if (__t_uobj == type_go) //如果绑定类型是GameObject,尝试寻找想要被注入的类型是不是这个GameObject上的Component { if (((UnityEngine.GameObject)uobj).TryGetComponent(property.PropertyType, out var _result)) { property.SetValue(injected_obj, _result); continue; } } } } } //BaseType Inject if (component.TryGetBindingType(find_bind_name, out var obj)) { if (obj.GetType() == property.PropertyType) { property.SetValue(injected_obj, obj); continue; } } if (!null_able) { throw new Exception($"Inject binding object failed: cannot found binding object \"{find_bind_name}\""); } } foreach (var field in t_obj.GetRuntimeFields()) { if (field.IsPrivate) { continue; } string find_bind_name = field.Name; bool null_able = true; var attr = field.GetCustomAttribute <BindingAttribute>(true); if (attr != null) { null_able = attr.Nullable; if (!string.IsNullOrEmpty(attr.BindingName)) { find_bind_name = attr.BindingName; } } if (field.FieldType.IsSubclassOf(type_uobj)) { if (component.TryGetBindingUObject(find_bind_name, out var uobj)) { Type __t_uobj = uobj.GetType(); if (__t_uobj.IsSubclassOf(field.FieldType) || __t_uobj.IsAssignableFrom(field.FieldType)) { field.SetValue(injected_obj, uobj); continue; } else { if (__t_uobj == type_go) //如果绑定类型是GameObject,尝试寻找想要被注入的类型是不是这个GameObject上的Component { if (((UnityEngine.GameObject)uobj).TryGetComponent(field.FieldType, out var _result)) { field.SetValue(injected_obj, _result); continue; } } } } } //BaseType Inject if (component.TryGetBindingType(find_bind_name, out var obj)) { if (obj.GetType() == field.FieldType) { field.SetValue(injected_obj, obj); continue; } } if (!null_able) { throw new Exception($"Inject binding object failed: cannot found binding object \"{find_bind_name}\""); } } }