diff --git a/Editor/UI/GenerateWindow/UIGenerateQuick.cs b/Editor/UI/GenerateWindow/UIGenerateQuick.cs index fe16528..8cd3e41 100644 --- a/Editor/UI/GenerateWindow/UIGenerateQuick.cs +++ b/Editor/UI/GenerateWindow/UIGenerateQuick.cs @@ -9,7 +9,7 @@ namespace AlicizaX.UI.Editor public static class UIGenerateQuick { [MenuItem("GameObject/UI生成绑定", priority = 10)] - public static void ShowWindow() + public static void UIGenerateBind() { GameObject selectedObject = Selection.gameObjects.FirstOrDefault(); if (selectedObject == null) diff --git a/Editor/UI/Helper/IUIGeneratorRuleHelper.cs b/Editor/UI/Helper/IUIGeneratorRuleHelper.cs index 026a130..81e916c 100644 --- a/Editor/UI/Helper/IUIGeneratorRuleHelper.cs +++ b/Editor/UI/Helper/IUIGeneratorRuleHelper.cs @@ -20,7 +20,7 @@ namespace AlicizaX.UI.Editor string GetUIResourceSavePath(GameObject targetObject, UIScriptGenerateData scriptGenerateData); - void WriteUIScriptContent(string className, string scriptContent, UIScriptGenerateData scriptGenerateData); + void WriteUIScriptContent(GameObject targetObject, string className, string scriptContent, UIScriptGenerateData scriptGenerateData); bool CheckCanGenerate(GameObject targetObject, UIScriptGenerateData scriptGenerateData); @@ -125,7 +125,7 @@ namespace AlicizaX.UI.Editor return Path.ChangeExtension(relPath, null); } - public void WriteUIScriptContent(string className, string scriptContent, UIScriptGenerateData scriptGenerateData) + public void WriteUIScriptContent(GameObject targetObject, string className, string scriptContent, UIScriptGenerateData scriptGenerateData) { if (string.IsNullOrEmpty(className)) throw new ArgumentNullException(nameof(className)); if (scriptContent == null) throw new ArgumentNullException(nameof(scriptContent)); @@ -136,6 +136,8 @@ namespace AlicizaX.UI.Editor Directory.CreateDirectory(scriptFolderPath); + scriptContent = scriptContent.Replace("#Controller#", string.Empty); + if (File.Exists(scriptFilePath) && IsContentUnchanged(scriptFilePath, scriptContent)) { UIScriptGeneratorHelper.BindUIScript(); diff --git a/Editor/UI/Helper/UIScriptGeneratorHelper.cs b/Editor/UI/Helper/UIScriptGeneratorHelper.cs index 723fbc8..a6fa65f 100644 --- a/Editor/UI/Helper/UIScriptGeneratorHelper.cs +++ b/Editor/UI/Helper/UIScriptGeneratorHelper.cs @@ -36,7 +36,16 @@ namespace AlicizaX.UI.Editor return typeof(GameObject); } - return Objs.FirstOrDefault()?.GetComponent(TypeName).GetType(); + try + { + return Objs.FirstOrDefault()?.GetComponent(TypeName).GetType(); + } + catch (Exception e) + { + Debug.Log(e); + } + + return null; } public UIBindData(string name, List objs, string typeName = "", EBindType bindType = EBindType.None) @@ -53,15 +62,25 @@ namespace AlicizaX.UI.Editor } } - internal static class UIScriptGeneratorHelper + public static class UIScriptGeneratorHelper { private static UIGenerateConfiguration _uiGenerateConfiguration; private static IUIGeneratorRuleHelper _uiGeneratorRuleHelper; private static readonly List _uiBindDatas = new List(); private static readonly HashSet _arrayComponents = new HashSet(StringComparer.Ordinal); - private static IUIGeneratorRuleHelper UIGeneratorRuleHelper => - _uiGeneratorRuleHelper ?? InitializeRuleHelper(); + private static IUIGeneratorRuleHelper UIGeneratorRuleHelper + { + get + { + if (_uiGeneratorRuleHelper == null || (_uiGeneratorRuleHelper != null && _uiGeneratorRuleHelper.GetType().FullName != UIConfiguration.UIScriptGeneratorRuleHelper)) + { + InitializeRuleHelper(); + } + + return _uiGeneratorRuleHelper; + } + } private static UIGenerateConfiguration UIConfiguration => _uiGenerateConfiguration ??= UIGenerateConfiguration.Instance; @@ -75,7 +94,7 @@ namespace AlicizaX.UI.Editor return null; } - var ruleHelperType = Type.GetType(ruleHelperTypeName); + var ruleHelperType = AlicizaX.Utility.Assembly.GetType(ruleHelperTypeName); if (ruleHelperType == null) { Debug.LogError($"UIScriptGeneratorHelper: Could not load UI ScriptGeneratorHelper {ruleHelperTypeName}"); @@ -240,7 +259,7 @@ namespace AlicizaX.UI.Editor return; } - _uiBindDatas.Add(new UIBindData(keyName, component.gameObject, component.name, EBindType.Widget)); + _uiBindDatas.Add(new UIBindData(keyName, component.gameObject, component.GetType().FullName, EBindType.Widget)); } private static void CollectArrayComponent(List arrayNode, string nodeName) @@ -359,9 +378,8 @@ namespace AlicizaX.UI.Editor { var templateText = File.ReadAllText(UIGlobalPath.TemplatePath); var processedText = ProcessTemplateText(targetObject, templateText, className, scriptGenerateData, ruleHelper); - - ruleHelper.WriteUIScriptContent(className, processedText, scriptGenerateData); EditorPrefs.SetString("Generate", className); + ruleHelper.WriteUIScriptContent(targetObject, className, processedText, scriptGenerateData); } private static string ProcessTemplateText(GameObject targetObject, string templateText, string className, UIScriptGenerateData scriptGenerateData, IUIGeneratorRuleHelper ruleHelper) @@ -394,8 +412,19 @@ namespace AlicizaX.UI.Editor _arrayComponents.Clear(); CollectBindData(targetObject.transform); - BindScriptPropertyField(targetObject, className); - CleanupContext(); + try + { + BindScriptPropertyField(targetObject, className); + } + catch (Exception e) + { + } + finally + { + CleanupContext(); + } + + EditorUtility.SetDirty(targetObject); Debug.Log($"Generate {className} Successfully attached to game object"); } @@ -434,7 +463,11 @@ namespace AlicizaX.UI.Editor private static void BindFieldsToComponents(Component targetHolder, Type scriptType) { - var fields = scriptType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo[] allNonPublicInstanceFields = scriptType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); + + var fields = allNonPublicInstanceFields.Where(field => + field.GetCustomAttributes(typeof(SerializeField), false).Length > 0 + ); foreach (var field in fields.Where(field => !string.IsNullOrEmpty(field.Name))) { diff --git a/Editor/UI/Res/UITemplate.txt b/Editor/UI/Res/UITemplate.txt index 43f2a48..90a244d 100644 --- a/Editor/UI/Res/UITemplate.txt +++ b/Editor/UI/Res/UITemplate.txt @@ -11,5 +11,7 @@ namespace #ClassNameSpace# #Variable# #endregion + +#Controller# } } diff --git a/Editor/UI/UIConfig/UIGenerateConfiguration.cs b/Editor/UI/UIConfig/UIGenerateConfiguration.cs index ef0ed52..4784d6b 100644 --- a/Editor/UI/UIConfig/UIGenerateConfiguration.cs +++ b/Editor/UI/UIConfig/UIGenerateConfiguration.cs @@ -12,7 +12,7 @@ using UnityEngine.Serialization; namespace AlicizaX.UI.Editor { [AlicizaX.Editor.Setting.FilePath("ProjectSettings/UIGenerateConfiguration.asset")] - internal class UIGenerateConfiguration : AlicizaX.Editor.Setting.ScriptableSingleton + public class UIGenerateConfiguration : AlicizaX.Editor.Setting.ScriptableSingleton { [Header("通用生成配置")] public UIGenerateCommonData UIGenerateCommonData = new UIGenerateCommonData(); diff --git a/Runtime/UI/UIBase/UIHolderObjectBase.cs b/Runtime/UI/UIBase/UIHolderObjectBase.cs index 5382511..6a553df 100644 --- a/Runtime/UI/UIBase/UIHolderObjectBase.cs +++ b/Runtime/UI/UIBase/UIHolderObjectBase.cs @@ -56,7 +56,7 @@ namespace AlicizaX.UI.Runtime #endif - private void Awake() + public virtual void Awake() { _target = gameObject; #if ALICIZAX_UI_ANIMATION_SUPPORT