当前位置:网站首页>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");
}
}
边栏推荐
- 引擎开发日志:OpenGL资源多线程加载
- LT8918L LVDS转MIPI芯片技术支持资料
- idea中创建jsp项目详细步骤
- 机械臂运动学解析
- 开源代码交叉编译操作流程及遇到的问题解决(lightdm)
- 2019 - ICCV - 图像修复 Image Inpainting 论文导读《StructureFlow: Image Inpainting via Structure-aware ~~》
- Case | industrial iot solutions, steel mills high-performance security for wisdom
- 电子密码锁_毕设‘指导’
- 【MQ-3 Alcohol Detector and Arduino Detect Alcohol】
- Mac安装MySQL详细教程
猜你喜欢
随机推荐
Introduction and mock implementation of list:list
STM32F4 CAN 配置注意的细节问题
滑动窗口方法
HDMI转MIPI CSI东芝转换芯片-TC358743XBG/TC358749XBG
【Connect the heart rate sensor to Arduino to read the heart rate data】
分割回文串 DP+回溯 (LeetCode-131)
HAL库笔记——通过按键来控制LED(基于正点原子STM32F103ZET6精英板)
发布全新的配置格式 - AT
Lightly 支持 Markdown 文件在线编写(文中提供详细 Markdown 语法)
剑指Offer 64.求1+2+...+n 递归+&&
IDEA2021.2安装与配置(持续更新)
增量编译技术在Lightly中的实践
读取FBX文件踩坑清单
功率计,物联网,智能插座电路设计【毕业设计】
Typora use
[Arduino connects the clock module to display the time on LCD1602]
ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
开源代码交叉编译操作流程及遇到的问题解决(lightdm)
AD实战篇
使用buildroot制作根文件系统(龙芯1B使用)


![开源日志库 [log4c] 使用](/img/f5/cce26f5820155c537f2cd975bafbcd.png)






