unity 有时候找路径太麻烦 写了一个自动生成脚本的工具

using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine; public class Auto_BuildCode
{
private static string path_dic = Application.dataPath + "/Code";
private static string path_suffix = ".cs";
private static string space_one = " ";
private static string space_two = " "; private static Dictionary<string, string> compon_tranform = new Dictionary<string, string>();
private static Dictionary<string, string> compon_image = new Dictionary<string, string>();
private static Dictionary<string, string> compon_text = new Dictionary<string, string>();
private static Dictionary<string, string> compon_slider = new Dictionary<string, string>(); [MenuItem("Game/BuildCode")]
private static void BuildCode()
{
Object select = Selection.activeObject; if (select != null && select is GameObject)
{
GameObject prefab = (GameObject)select;
Get_All_Compon(prefab.transform);
string code_name = prefab.name;
Code_Create(code_name);
Code_Write(code_name);
} AssetDatabase.Refresh();
} private static void Get_All_Compon(Transform prefab)
{
Transform[] components = prefab.GetComponentsInChildren<Transform>();
for (int i = ; i < components.Length; i++)
{
string compon = components[i].name.Split('_')[];
string compon_path = string.Empty; switch (compon)
{
case "Transform":
compon_path = Get_Compon_Path(prefab, components[i]);
compon_tranform.Add(components[i].name, compon_path);
break;
case "Image":
compon_path = Get_Compon_Path(prefab, components[i]);
compon_image.Add(components[i].name, compon_path);
break;
case "Text":
compon_path = Get_Compon_Path(prefab, components[i]);
compon_text.Add(components[i].name, compon_path);
break;
case "Slider":
compon_path = Get_Compon_Path(prefab, components[i]);
compon_slider.Add(components[i].name, compon_path);
break;
default:
break;
}
}
} private static string Get_Compon_Path(Transform parent, Transform child)
{
StringBuilder sb = new StringBuilder(); while (true)
{
if (child.parent == null)
{
break;
}
else if (child.parent != parent)
{
sb.Insert(, child.name);
sb.Insert(, "/");
}
else
{
sb.Insert(, child.name);
break;
}
child = child.parent;
} return sb.ToString();
} private static void Code_Create(string code_name)
{
if (!Directory.Exists(path_dic))
{
Directory.CreateDirectory(path_dic);
} string path_file = path_dic + "/" + code_name + path_suffix; if (File.Exists(path_file))
{
File.Delete(path_file);
}
} private static void Code_Write(string code_name)
{
string path_file = path_dic + "/" + code_name + path_suffix; FileStream fs = new FileStream(path_file, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs); sw.WriteLine("using System;");
sw.WriteLine("using System.Collections;");
sw.WriteLine("using System.Collections.Generic;");
sw.WriteLine("using UnityEngine;");
sw.WriteLine("using UnityEngine.UI;");
sw.WriteLine("");
sw.WriteLine("public class " + code_name + " : MonoBehaviour");
sw.WriteLine("{"); #region 定义变量
foreach (var compon in compon_tranform)
{
sw.WriteLine(space_one + "public Transform " + compon.Key + ";");
} foreach (var compon in compon_image)
{
sw.WriteLine(space_one + "public Image " + compon.Key + ";");
} foreach (var compon in compon_text)
{
sw.WriteLine(space_one + "public Text " + compon.Key + ";");
} foreach (var compon in compon_slider)
{
sw.WriteLine(space_one + "public Slider " + compon.Key + ";");
}
#endregion sw.WriteLine(space_one + "private void Awake()");
sw.WriteLine(space_one + "{"); #region 对UI变量进行赋值
foreach (var compon in compon_tranform)
{
sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ");");
} foreach (var compon in compon_image)
{
sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ").GetComponent<Image>();");
} foreach (var compon in compon_text)
{
sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ").GetComponent<Text>();");
} foreach (var compon in compon_slider)
{
sw.WriteLine(space_two + compon.Key + " = " + "transform.Find(" + '"' + compon.Value + '"' + ").GetComponent<Slider>();");
}
#endregion sw.WriteLine(space_one + "}");
sw.WriteLine("}"); sw.Dispose();
fs.Dispose();
}
}

unity组件路径自动生成的更多相关文章

  1. Unity UI代码自动生成

    最近在做新项目跟同事讨论UI制作方案, 这里就说下根据节点来生成UI代码,  这个工具可以根据预设生成一个分布类.目前组件还不是很完善, 自己使用需要修改部分代码 组件功能如下: 1. 自动设置引用 ...

  2. 将文件夹中的图像路径自动生成txt文件(便于opencv遍历处理图像)

    代码: #include<iostream> #include<vector> #include<io.h> #include<fstream> usi ...

  3. DirectX10安装路径自动生成DXSDK_DIR

    DXSDK_DIR C:\Program Files (x86)\Microsoft DirectX SDK (February 2010)\

  4. Unity 自动生成组件索引类工具

    Unity 自动生成组件索引类工具 需求由来 我们在写UI类时 需要获取预设中的组件 joystick = transform.Find("joystick"); backgrou ...

  5. 自动生成查找组件的lua代码

    本篇主要解决的问题是使用lua脚本编写unity业务逻辑时,自动生成一些查找组件及绑定控件事件的lua代码! 现在很多unity项目都是用ulua作为热更新解决方案,因此需要用lua来写相关的逻辑,经 ...

  6. 组件化框架设计之apt编译时期自动生成代码&动态类加载(二)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 本篇文章将继续从以下两个内容来介绍组件化框架设计: apt编译时 ...

  7. wix在使用heat自动生成wxs时添加windows服务组件

    最近需要给安装包增加一个windows服务组件,按照我的理解,我以为只需要Product.wxs加一段如下的标签就可以了 <Componet Id="myservice"&g ...

  8. Unity自动生成AnimatorController

    上一篇写了如何自动切割动画,这一篇写如何自动生成AnimatorController. 之前网上查了很多资料,看的一直很蒙,看不懂是怎么回事的,这里我先给大家明确几个概念: 画的不好,大家将就着看,写 ...

  9. Unity 导出的android项目自动生成Private Libraries

    如果Unity里面Plugins/Android 添加了 jar 文件,则导出Android 项目时会自动生成 Private Libraries. 而且里面的项还删不掉 然后在网上搜了一下,找到了原 ...

随机推荐

  1. Beta(5/7)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  2. rhel配置网络yum源

    redhat 7.2配置网易yum源 卸载原来的yum源和安装必须的rpm包 1)确保虚拟机能够连接外网 2)前往http://mirrors.163.com/centos/7/os/x86_64/P ...

  3. VB中Winsock连续发送出现接收不到的异常问题解决方法

    VB里面用WINSOCK进行一对多连接的TCP连接时,经常需要群发消息给所有已连接的客户端.代码类似如下: Option Explicit Dim bytMsg() As Byte Private S ...

  4. Java自学笔记

    1.标识符:字母,数字,下划线,美元符组成,不可以已数字开头,并且严格区分大小写 2.变量 有三个元素描述变量,变量类型,变量名,变量值 如何命名变量:首字母为 字母:下划线:“$“符号,其余部分为 ...

  5. OO第四次博客作业

    测试与正确性论证的效果差异及其优缺点 测试是利用测试代码,通过编写测试用例来验证代码是否能正常完成所要求的功能,自动测试相较于正确性论证来说更加的直观,直接测试代码的功能,而正确性论证是在JSF的基础 ...

  6. SourceTree安装跳过登录

    安装 SourceTree 时,需要使用atlassian授权,因为各种原因无法完成授权,现提供跳过 atlassian账号 授权方法. 安装之后,转到用户本地文件夹下的 SourceTree 目录, ...

  7. day 22 二十二、面向对象导入、名称空间、类与对象

    一.面向对象导入 1.含义: ①面向过程: 重过程:解决问题,考虑的是解决问题的流程 解决问题的思路清晰,但拓展性不强 ②面向对象: 重对象:解决问题,找到解决问题的对象 解决问题的思路可能不止一条( ...

  8. workerman 安装event 扩展

    2018年7月31日10:07:47 一些小技巧 命令行直接运行PHP代码 php -r "phpinfo();" 交互模式运行PHP php -a PHP脚本作为shell脚本运 ...

  9. linux权限字母的含义

    无 --- 只能列出文件 r-- 访问文件 r-x 创建和删除文件 rwx

  10. 报文分析4、TCP协议的头结构

    TCP协议的头结构 来源端口(2字节) 目的端口(2字节) 序号(4字节) 确认序号(4字节) 头长度(4位) 保留(6位) URG ACK PSH RST SYN PIN 窗口大小(2字节) 校验和 ...