using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.UI; using System.IO; using System.Text; public class PSDTOUGUIProcessor { [UnityEditor.MenuItem("Assets/PSDTOUGUI后处理")] public static void BaseSettings() { GameObject go = Selection.activeGameObject; if (go == null) { return; } // go.AddMissingComponent(); // go.AddMissingComponent(); // go.AddMissingComponent(); } [UnityEditor.MenuItem("Assets/新UI处理")] public static void NewUIHandle() { GameObject go = Selection.activeGameObject; if (go == null) { return; } if (!go.name.EndsWith("Win")) { Debug.LogError("请选择正确的UI UI应该以Win结尾"); return; } go.AddMissingComponent(); go.AddMissingComponent(); go.AddMissingComponent(); var graphics = go.GetComponentsInChildren(true); foreach (Graphic g in graphics) { g.raycastTarget = false; } } private static List SplitByCamelCase(string input) { if (string.IsNullOrEmpty(input)) return new List(); var result = new List(); var currentWord = new StringBuilder(); currentWord.Append(input[0]); for (int i = 1; i < input.Length; i++) { if (char.IsUpper(input[i])) { // 遇到大写字母,表示新单词开始 result.Add(currentWord.ToString()); currentWord.Clear(); } currentWord.Append(input[i]); } // 添加最后一个单词 if (currentWord.Length > 0) { result.Add(currentWord.ToString()); } return result; } [UnityEditor.MenuItem("GameObject/生成UI脚本", false, 10)] public static void GenerateUIScript() { GameObject go = Selection.activeGameObject; if (go == null) { Debug.LogError("请先选择一个GameObject"); return; } string className = go.name; List caseList = SplitByCamelCase(className); string targetFolder = Path.Combine(Application.dataPath, "Scripts/Main/System"); if (Directory.Exists(targetFolder + "/" + caseList[0])) { targetFolder += "/" + caseList[0]; } // 确保目标文件夹存在 if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } string filePath = Path.Combine(targetFolder, className + ".cs"); // 检查文件是否已存在 if (File.Exists(filePath)) { if (!EditorUtility.DisplayDialog("文件已存在", $"文件 {className}.cs 已存在,是否覆盖?", "覆盖", "取消")) { return; } } // 生成脚本内容 StringBuilder sb = new StringBuilder(); sb.AppendLine("using UnityEngine;"); sb.AppendLine("using System.Collections;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using UnityEngine.UI;"); sb.AppendLine("using DG.Tweening;"); sb.AppendLine("using Cysharp.Threading.Tasks;"); sb.AppendLine(""); sb.AppendLine($"public class {className} : UIBase"); sb.AppendLine("{"); sb.AppendLine(" // 组件引用"); sb.AppendLine(""); sb.AppendLine(" // 生命周期"); sb.AppendLine(" protected override void InitComponent()"); sb.AppendLine(" {"); sb.AppendLine(" base.InitComponent();"); sb.AppendLine(" // 初始化组件引用 绑定按钮等UI组件事件"); sb.AppendLine(" }"); sb.AppendLine(""); sb.AppendLine(" protected override void OnPreOpen()"); sb.AppendLine(" {"); sb.AppendLine(" base.OnPreOpen();"); sb.AppendLine(" }"); sb.AppendLine(""); sb.AppendLine(" protected override void OnPreClose()"); sb.AppendLine(" {"); sb.AppendLine(" base.OnPreClose();"); sb.AppendLine(" }"); sb.AppendLine(""); sb.AppendLine(" protected override void OnOpen()"); sb.AppendLine(" {"); sb.AppendLine(" base.OnOpen();"); sb.AppendLine(" }"); sb.AppendLine(""); sb.AppendLine(" protected override void OnClose()"); sb.AppendLine(" {"); sb.AppendLine(" base.OnClose();"); sb.AppendLine(" }"); sb.AppendLine(""); sb.AppendLine(" protected override void NextFrameAfterOpen()"); sb.AppendLine(" {"); sb.AppendLine(" base.NextFrameAfterOpen();"); sb.AppendLine(" }"); sb.AppendLine(""); sb.AppendLine(" protected override void CompleteClose()"); sb.AppendLine(" {"); sb.AppendLine(" base.CompleteClose();"); sb.AppendLine(" }"); sb.AppendLine("}"); // 写入文件 File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); Debug.Log($"UI脚本已生成: {filePath}"); // 刷新资源数据库以显示新文件 AssetDatabase.Refresh(); // 打开生成的脚本 UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(filePath, 1); } }