using System; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using System.IO; public class UICtrlGenerateWindow:EditorWindow { public GameObject selectObj; //要生成脚本的UI预设 public bool isCreateDir; //是否新建文件夹 public string createInDir; private GameObject _lastGo; private string _createDir; private string _curDir; private bool _isAdmitRepeat; [MenuItem("Tools/*UI/UI脚本自动生成工具", false, (int)NLDMenuID.UITools)] static void ShowWindow() { UICtrlGenerateWindow window = (UICtrlGenerateWindow)EditorWindow.GetWindow(typeof(UICtrlGenerateWindow)); if (Selection.gameObjects.Length > 0 && Selection.gameObjects[0]) { window.selectObj = Selection.gameObjects[0]; } } static void ShowWindow(GameObject go) { UICtrlGenerateWindow window = (UICtrlGenerateWindow)EditorWindow.GetWindow(typeof(UICtrlGenerateWindow)); window.selectObj = go; } public static void ShowGenerateWindow(GameObject go = null) { ShowWindow(); } private void OnGUI() { GUILayout.BeginVertical(); GUILayout.Space(8); GUI.skin.label.fontSize = 18; GUI.skin.label.alignment = TextAnchor.MiddleCenter; GUILayout.Label("UI代码生成工具"); //默认路径是 GUILayout.Space(10); GUI.skin.label.fontSize = 12; GUI.skin.label.alignment = TextAnchor.MiddleLeft; GUILayout.Label("生成路径:"); GUILayout.BeginHorizontal(); GUILayout.Space(5); string defaultPath = GetUIPath.filePath; string curPath = ""; string fileName = ""; if (createInDir != null) { createInDir.Replace('\\', '/'); curPath = createInDir; } if (selectObj) { if (_lastGo != selectObj) { fileName = selectObj.name + "Controller.cs"; _lastGo = selectObj; if (createInDir == null || createInDir.Length <= 0) { createInDir = defaultPath; curPath = createInDir; } if (curPath.EndsWith("/")) { curPath += fileName; } else if (curPath.EndsWith(".cs")) { int idx = curPath.Length - 1; while (!curPath[idx].Equals('/')) { idx -= 1; } curPath = curPath.Remove(idx + 1); curPath += fileName; } else { curPath += '/'; curPath += fileName; } createInDir = curPath; } createInDir.Replace('\\', '/'); } else { if (createInDir == null || createInDir.Length <= 0) { createInDir = defaultPath; } createInDir.Replace('\\', '/'); } createInDir.Replace('\\', '/'); createInDir = EditorGUILayout.TextArea(createInDir, GUILayout.MinWidth(150)); GUILayout.EndHorizontal(); GUILayout.Space(10); selectObj = (GameObject)EditorGUILayout.ObjectField("选中的UIPrefab:", selectObj, typeof(GameObject), true, GUILayout.MinWidth(100)); GUILayout.Space(10); var newVal = EditorGUILayout.Toggle("是否允许重名", _isAdmitRepeat); _isAdmitRepeat = newVal; // GUILayout.Space(10); // isCreateDir = EditorGUILayout.Toggle("是否新建文件夹", isCreateDir); GUILayout.Space(20); if (GUILayout.Button("生成", GUILayout.Width(80))) { Genarate(); } GUILayout.EndVertical(); } void Genarate() { if (selectObj) { GetUIPath.CreateScript(selectObj, createInDir, _isAdmitRepeat); } } }