diff --git a/Editor/UI/Helper/IUIGeneratorRuleHelper.cs b/Editor/UI/Helper/IUIGeneratorRuleHelper.cs index 7119020..026a130 100644 --- a/Editor/UI/Helper/IUIGeneratorRuleHelper.cs +++ b/Editor/UI/Helper/IUIGeneratorRuleHelper.cs @@ -181,8 +181,8 @@ namespace AlicizaX.UI.Editor } uiBindDatas? - .Where(bindData => bindData?.BindCom?.FirstOrDefault() != null) - .Select(bindData => bindData.BindCom[0].GetType().Namespace) + .Where(bindData => bindData?.Objs?.FirstOrDefault() != null) + .Select(bindData => bindData.GetFirstOrDefaultType().Namespace) .Where(ns => !string.IsNullOrEmpty(ns)) .ToList() .ForEach(ns => namespaceSet.Add(ns)); @@ -207,7 +207,7 @@ namespace AlicizaX.UI.Editor { var variableName = bindData.Name; var publicName = GetPublicComponentByNameRule(variableName); - var firstType = bindData.BindCom?.FirstOrDefault()?.GetType(); + var firstType = bindData.GetFirstOrDefaultType(); var typeName = firstType?.Name ?? "Component"; var declaration = new StringBuilder(); @@ -222,7 +222,7 @@ namespace AlicizaX.UI.Editor break; case EBindType.ListCom: - var count = Math.Max(0, bindData.BindCom?.Count ?? 0); + var count = Math.Max(0, bindData.Objs?.Count ?? 0); declaration.AppendLine($"\t\tprivate {typeName}[] {variableName} = new {typeName}[{count}];"); declaration.Append($"\t\tpublic {typeName}[] {publicName} => {variableName};"); break; diff --git a/Editor/UI/Helper/UIScriptGeneratorHelper.cs b/Editor/UI/Helper/UIScriptGeneratorHelper.cs index 3f06bff..723fbc8 100644 --- a/Editor/UI/Helper/UIScriptGeneratorHelper.cs +++ b/Editor/UI/Helper/UIScriptGeneratorHelper.cs @@ -22,18 +22,33 @@ namespace AlicizaX.UI.Editor public class UIBindData { public string Name { get; } - public List BindCom { get; } - public EBindType BindType { get; } - public UIBindData(string name, List bindCom, EBindType bindType = EBindType.None) + public List Objs { get; set; } + public EBindType BindType { get; } + public bool IsGameObject => nameof(GameObject).Equals(TypeName); + + public string TypeName = string.Empty; + + public Type GetFirstOrDefaultType() { - Name = name ?? throw new ArgumentNullException(nameof(name)); - BindCom = bindCom ?? new List(); - BindType = bindType; + if (IsGameObject) + { + return typeof(GameObject); + } + + return Objs.FirstOrDefault()?.GetComponent(TypeName).GetType(); } - public UIBindData(string name, Component bindCom, EBindType bindType = EBindType.None) - : this(name, new List { bindCom }, bindType) + public UIBindData(string name, List objs, string typeName = "", EBindType bindType = EBindType.None) + { + Name = name; + Objs = objs ?? new List(); + BindType = bindType; + TypeName = typeName; + } + + public UIBindData(string name, GameObject obj, string typeName = "", EBindType bindType = EBindType.None) + : this(name, new List { obj }, typeName, bindType) { } } @@ -176,11 +191,16 @@ namespace AlicizaX.UI.Editor var typeName = GetUIElementComponentType(com); if (string.IsNullOrEmpty(typeName)) continue; - var component = node.GetComponent(typeName); - if (component == null) + + bool isGameObject = typeName.Equals(nameof(GameObject)); + if (!isGameObject) { - Debug.LogError($"{node.name} does not have component of type {typeName}"); - continue; + var component = node.GetComponent(typeName); + if (component == null) + { + Debug.LogError($"{node.name} does not have component of type {typeName}"); + continue; + } } var keyName = UIGeneratorRuleHelper.GetPrivateComponentByNameRule(com, node.name, EBindType.None); @@ -190,7 +210,7 @@ namespace AlicizaX.UI.Editor continue; } - _uiBindDatas.Add(new UIBindData(keyName, component)); + _uiBindDatas.Add(new UIBindData(keyName, node.gameObject, typeName)); } } @@ -220,7 +240,7 @@ namespace AlicizaX.UI.Editor return; } - _uiBindDatas.Add(new UIBindData(keyName, component, EBindType.Widget)); + _uiBindDatas.Add(new UIBindData(keyName, component.gameObject, component.name, EBindType.Widget)); } private static void CollectArrayComponent(List arrayNode, string nodeName) @@ -256,7 +276,7 @@ namespace AlicizaX.UI.Editor return componentArray.Select((com, index) => { var keyName = UIGeneratorRuleHelper.GetPrivateComponentByNameRule(com, nodeName, EBindType.ListCom); - return new UIBindData(keyName, new List(), EBindType.ListCom); + return new UIBindData(keyName, new List(), com, EBindType.ListCom); }).ToList(); } @@ -269,13 +289,15 @@ namespace AlicizaX.UI.Editor var typeName = GetUIElementComponentType(com); if (string.IsNullOrEmpty(typeName)) continue; - + tempBindDatas[index].TypeName = typeName; foreach (var node in orderedNodes) { - var component = node.GetComponent(typeName); - if (component != null) + var isGameObject = typeName.Equals(nameof(GameObject)); + var component = isGameObject ? null : node.GetComponent(typeName); + + if (component != null || isGameObject) { - tempBindDatas[index].BindCom.Add(component); + tempBindDatas[index].Objs.Add(node.gameObject); } else { @@ -396,8 +418,8 @@ namespace AlicizaX.UI.Editor return; } - var component = targetObject.GetOrAddComponent(scriptType); - BindFieldsToComponents(component, scriptType); + var targetHolder = targetObject.GetOrAddComponent(scriptType); + BindFieldsToComponents(targetHolder, scriptType); } private static Type FindScriptType(string scriptClassName) @@ -410,36 +432,37 @@ namespace AlicizaX.UI.Editor type.Name.Equals(scriptClassName, StringComparison.Ordinal)); } - private static void BindFieldsToComponents(Component component, Type scriptType) + private static void BindFieldsToComponents(Component targetHolder, Type scriptType) { var fields = scriptType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (var field in fields.Where(field => !string.IsNullOrEmpty(field.Name))) { - var components = _uiBindDatas.Find(data => data.Name == field.Name)?.BindCom; + var bindData = _uiBindDatas.Find(data => data.Name == field.Name); + var components = bindData.Objs; if (components == null) { Debug.LogError($"Field {field.Name} did not find matching component binding"); continue; } - SetFieldValue(field, components, component); + SetFieldValue(field, components, bindData.TypeName, targetHolder); } } - private static void SetFieldValue(FieldInfo field, IReadOnlyList components, Component targetComponent) + private static void SetFieldValue(FieldInfo field, IReadOnlyList components, string typeName, Component targetComponent) { if (field.FieldType.IsArray) { - SetArrayFieldValue(field, components, targetComponent); + SetArrayFieldValue(field, components, typeName, targetComponent); } else { - SetSingleFieldValue(field, components, targetComponent); + SetSingleFieldValue(field, components, typeName, targetComponent); } } - private static void SetArrayFieldValue(FieldInfo field, IReadOnlyList components, Component targetComponent) + private static void SetArrayFieldValue(FieldInfo field, IReadOnlyList components, string typeName, Component targetComponent) { var elementType = field.FieldType.GetElementType(); if (elementType == null) @@ -453,9 +476,12 @@ namespace AlicizaX.UI.Editor { if (components[i] == null) continue; - if (elementType.IsInstanceOfType(components[i])) + var isGameobject = typeName.Equals(nameof(GameObject)); + object ComponentObject = isGameobject ? components[i] : components[i].GetComponent(typeName); + + if (elementType.IsInstanceOfType(ComponentObject)) { - array.SetValue(components[i], i); + array.SetValue(ComponentObject, i); } else { @@ -466,11 +492,12 @@ namespace AlicizaX.UI.Editor field.SetValue(targetComponent, array); } - private static void SetSingleFieldValue(FieldInfo field, IReadOnlyList components, Component targetComponent) + private static void SetSingleFieldValue(FieldInfo field, IReadOnlyList components, string typeName, Component targetComponent) { if (components.Count == 0) return; - var firstComponent = components[0]; + var isGameobject = typeName.Equals(nameof(GameObject)); + object firstComponent = isGameobject ? components[0] : components[0].GetComponent(typeName); if (firstComponent == null) return; if (field.FieldType.IsInstanceOfType(firstComponent))