当前位置:网站首页>unity学习(一):自动化创建模板脚本
unity学习(一):自动化创建模板脚本
2022-08-02 03:34:00 【落水无痕】
在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");
}
}
边栏推荐
猜你喜欢
全加器高进位和低进位的理解
使用pyqt弹出消息提示框
Type c PD 电路设计
MQ-5 combustible gas sensor interface with Arduino
Host your own website with Vercel
与TI的lvds芯片兼容-GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片,
联阳(ITE)IT66021FN:HDMI转RGB芯片 3D 资料
【plang1.4.3】编写水母动画脚本
Lightly 支持 Markdown 文件在线编写(文中提供详细 Markdown 语法)
如何用 Lightly 进行 Debug 断点调试?
随机推荐
USB_ID介绍
谷粒商城10——搜索、商品详情、异步编排
为什么D类音频功放可以免输出滤波器
【plang 1.4.4】编写茶几玛丽脚本
引擎开发日志:OpenGL资源多线程加载
【plang 1.4.4】编写贪吃蛇脚本
关于IIC SDA毛刺的那些事
STM32 CAN 介绍以及相关配置
剑指Offer 32.Ⅰ从上到下打印二叉树
Comparative analysis of OneNET Studio and IoT Studio
【plang1.4.3】编写水母动画脚本
【LeetCode】合并
哈希表解题方法
【详解】线程池及其自定义线程池的实现
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
Based on the raspberry pie smart luggage development environment set up
字符串匹配(蛮力法+KMP)
Process (below): process control, termination, waiting, replacement
NSIS来自己设定快捷方式的图标
IDEA2021.2安装与配置(持续更新)