This commit is contained in:
陈思海 2025-12-01 16:45:42 +08:00
parent 33922c9521
commit 0045d5a7e1
6 changed files with 53 additions and 16 deletions

View File

@ -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)

View File

@ -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();

View File

@ -36,8 +36,17 @@ namespace AlicizaX.UI.Editor
return typeof(GameObject);
}
try
{
return Objs.FirstOrDefault()?.GetComponent(TypeName).GetType();
}
catch (Exception e)
{
Debug.Log(e);
}
return null;
}
public UIBindData(string name, List<GameObject> 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<UIBindData> _uiBindDatas = new List<UIBindData>();
private static readonly HashSet<string> _arrayComponents = new HashSet<string>(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<Transform> 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);
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)))
{

View File

@ -11,5 +11,7 @@ namespace #ClassNameSpace#
#Variable#
#endregion
#Controller#
}
}

View File

@ -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<UIGenerateConfiguration>
public class UIGenerateConfiguration : AlicizaX.Editor.Setting.ScriptableSingleton<UIGenerateConfiguration>
{
[Header("通用生成配置")] public UIGenerateCommonData UIGenerateCommonData = new UIGenerateCommonData();

View File

@ -56,7 +56,7 @@ namespace AlicizaX.UI.Runtime
#endif
private void Awake()
public virtual void Awake()
{
_target = gameObject;
#if ALICIZAX_UI_ANIMATION_SUPPORT