在Editor文件加中加入UIAutoGenWin.cs
编辑窗口
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
/*
* 框架功能窗口
*/
public class UIAutoGenWin : EditorWindow {
[MenuItem("framework/生成UI通用模板")]
static void run() {
EditorWindow.GetWindow<UIAutoGenWin>();
}
void OnGUI() {
GUILayout.Label("选择一个UI 视图根节点");
if (GUILayout.Button("生成代码")) {
if (Selection.gameObjects.Length > 0)
{
for (int i = 0; i < Selection.gameObjects.Length; i++)
{
Debug.Log("开始生成..."+ Selection.gameObjects[i].name);
CreatUISourceUtil.CreatUISourceFile(Selection.gameObjects[i]);
Debug.Log("生成结束" + Selection.gameObjects[i].name);
}
}
}
if (Selection.gameObjects.Length>0)
{
for (int i = 0; i < Selection.gameObjects.Length; i++)
{
GUILayout.Label(Selection.gameObjects[i].name);
}
}
else
{
GUILayout.Label("没有选中的UI节点,无法生成");
}
// if (Selection.activeGameObject != null) {
// GUILayout.Label(Selection.activeGameObject.name);
// }
// else {
// GUILayout.Label("没有选中的UI节点,无法生成");
// }
}
void OnSelectionChange() {
this.Repaint();
}
}
在Editor文件加中加入CreatUISourceUtil.cs
//生成模板脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
/*
* 创建模板脚本
*/
public class CreatUISourceUtil {
//创建UISource文件的函数
public static void CreatUISourceFile(GameObject selectGameObject)
{
string gameObjectName = selectGameObject.name;
// string fileName = gameObjectName + "_UISrc";
string className = gameObjectName + "_UICtrl";
StreamWriter sw = null;
/*sw = new StreamWriter(Application.dataPath + "/Scripts/UI_auto_gen/" + fileName + ".cs");
sw.WriteLine(
"using UnityEngine;\nusing System.Collections;\nusing UnityEngine.UI;\nusing System.Collections.Generic;");
sw.WriteLine("///UISource File Create Data: " + System.DateTime.Now.ToString());
sw.WriteLine("public partial class " + className + " : MonoBehaviour {" + "\n");
sw.WriteLine("\t" + "public Dictionary<string, GameObject> view = new Dictionary<string, GameObject>();");
sw.WriteLine("\t" + "void load_all_object(GameObject root, string path) {");
sw.WriteLine("\t\t" + "foreach (Transform tf in root.transform) {");
sw.WriteLine("\t\t\t" + "if (this.view.ContainsKey(path + tf.gameObject.name)) {");
sw.WriteLine("\t\t\t\t" + "Debug.LogWarning(\"Warning object is exist:\" + path + tf.gameObject.name + \"!\");");
sw.WriteLine("\t\t\t\t" + "continue;");
sw.WriteLine("\t\t\t" + "}");
sw.WriteLine("\t\t\t" + "this.view.Add(path + tf.gameObject.name, tf.gameObject);");
sw.WriteLine("\t\t\t" + "load_all_object(tf.gameObject, path + tf.gameObject.name + \"/\");");
sw.WriteLine("\t\t" + "}" + "\n");
sw.WriteLine("\t" + "}" + "\n");
sw.WriteLine("\t" + "void Awake() {");
sw.WriteLine("\t\t" + "this.load_all_object(this.gameObject, \"\");" + "\n");
sw.WriteLine("\t" + "}" + "\n");
sw.WriteLine("}");
sw.Flush();
sw.Close();
Debug.Log("Gen: " + Application.dataPath + "/Scripts/UI_auto_gen/" + fileName + ".cs");
*/
if (Directory.Exists(Application.dataPath + "/Scripts/UIControllers")==false)
{
Directory.CreateDirectory(Application.dataPath + "/Scripts/UIControllers");
}
if (File.Exists(Application.dataPath + "/Scripts/UIControllers/" + className + ".cs")) {
return;
}
sw = new StreamWriter(Application.dataPath + "/Scripts/UIControllers/" + className + ".cs");
sw.WriteLine(
"using UnityEngine;\nusing System.Collections;\nusing UnityEngine.UI;\nusing System.Collections.Generic;");
//sw.WriteLine("public class " + className + " : UI_ctrl {" + "\n");
sw.WriteLine("public class " + className + " : MonoBehaviour {" + "\n");
sw.WriteLine("\t" + "public void Awake() {");
//sw.WriteLine("\t" + "public override void Awake() {");
//sw.WriteLine("\t\t" + "base.Awake();" + "\n");
sw.WriteLine("\t" + "}" + "\n");
sw.WriteLine("\t" + "void Start() {");
sw.WriteLine("\t" + "}" + "\n");
sw.WriteLine("}");
sw.Flush();
sw.Close();
Debug.Log("Gen: " + Application.dataPath + "/Scripts/UIControllers/" + className + ".cs");
}
}