当前位置:网站首页>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");
}
}
边栏推荐
- 与TI的lvds芯片兼容-GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片,
- 实现动态库(DLL)之间内存统一管理
- 使用buildroot制作根文件系统(龙芯1B使用)
- MQ-5 combustible gas sensor interface with Arduino
- idea中创建jsp项目详细步骤
- 联阳(ITE)IT66021FN:HDMI转RGB芯片 3D 资料
- openwrt RK3568_EVB移植
- Introduction and mock implementation of list:list
- 谷粒商城10——搜索、商品详情、异步编排
- NE5532运放加法器
猜你喜欢

蛮力法求解凸包问题

Compatible with C51 and STM32 Keil5 installation method

如何用 Lightly 进行 Debug 断点调试?

Comparative analysis of OneNET Studio and IoT Studio

MQ-5 combustible gas sensor interface with Arduino

【plang 1.4.3】定时器的使用

使用pyqt弹出消息提示框

【LeetCode】求和

2019 - ICCV - 图像修复 Image Inpainting 论文导读《StructureFlow: Image Inpainting via Structure-aware ~~》

增量编译技术在Lightly中的实践
随机推荐
PCIE电路设计
机械臂运动学解析
分割回文串 DP+回溯 (LeetCode-131)
openwrt RK3568_EVB移植
[Arduino connected to GP2Y1014AU0F dust sensor]
How to quickly build your own IoT platform?
AD实战篇
LT8918L LVDS转MIPI芯片技术支持资料
关于IIC SDA毛刺的那些事
调试九法准则
【Popular Science Post】Detailed explanation of MDIO interface
使用pyqt弹出消息提示框
D类音频功放NS4110B电路设计
Process (in): process state, process address space
本地数据库 sqlite3 编译和使用
引擎开发日志:集成Bullet3物理引擎
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
字符串匹配(蛮力法+KMP)
2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》
龙讯LT6911系列C/UXC/UXB/GXC/GXB芯片功能区别阐述