【Unity3D5.6版本使用(1)】自定义编辑器获取场景所有对象Tag生成Json
2017-07-31 14:02
591 查看
在unity3D5.x版本中,一部分以前的代码无法正常运行,比如:加载场景时,使用Application.loadedLevel会提示“已过时”,需要使用EditorSceneManager.OpenScene(FileName[scount]);所以写一篇记录下在新版本环境实现一些功能的笔记(一些没变化)。
一.获取当前场景所有Tag
想要获取场景(多个场景)的Tag,需要先加载场景,我是想要做一个插件,能让使用者一键获取项目中所有的场景,并读取场景中对应的Tag。那么就需要遍历项目中的场景,一般项目中都会把场景存放在Scenes文件夹下
![](https://img-blog.csdn.net/20170731120354892?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpeGluXzM4MDE0ODY1/font/5a6L5L2T/fontsize/1/fill/I0JBQkFCMA==/dissolve/50/gravity/SouthEast)
所以读取指定文件夹下所有文件即可
二.自定义编辑器
`[MenuItem(“MyMenu/getAllTag”)]
三.写本地Json
这里用到了一个Newtonsoft.Json的DLL来转换Json,注意版本最好使用2.0.
一.获取当前场景所有Tag
想要获取场景(多个场景)的Tag,需要先加载场景,我是想要做一个插件,能让使用者一键获取项目中所有的场景,并读取场景中对应的Tag。那么就需要遍历项目中的场景,一般项目中都会把场景存放在Scenes文件夹下
所以读取指定文件夹下所有文件即可
//获取文件路径 string DPath = Application.dataPath; int num = DPath.LastIndexOf("/"); DPath = DPath.Substring(0, num); DPath = DPath + "/Assets/Scenes"; //读取文件夹下的所有对象存到数组中 List<string> filename = new List<string>(); if (Directory.Exists(DPath)) { DirectoryInfo direction = new DirectoryInfo(DPath); FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories); for (int i = 0; i < files.Length; i++) { if (files[i].Name.EndsWith(".meta"))//不读取后缀为".meta"的文件 { continue; } filename.Add(files[i].FullName); } } string[] FileName = filename.ToArray(); for(int scount=0;scount< FileName.Length;scount++) { EditorSceneManager.OpenScene(FileName[scount]);//载入场景 GameObject[] AllObject = Resources.FindObjectsOfTypeAll<GameObject>(); List<string> arrlist = new List<string>(); for (int i = 0; i < AllObject.Length; i++) { arrlist.Add(AllObject[i].tag); } string[] AllTag = arrlist.ToArray(); AllTag = SelectString(AllTag, "mcitagdev"); udtDeviceMap[] Devicemap = null; for (int j = 0; j < AllTag.Length; j++) { if(Devicemap==null) { Array.Resize(ref Devicemap, 1); } else { int max = Devicemap.Length; Array.Resize(ref Devicemap,max+1); } string[] arrl = AllTag[j].Split('_'); string devname = arrl[1] +"_"+ arrl[2] + "_" + arrl[3] + "_" + arrl[4]; // Devicemap[Devicemap.Length-1]= udtDeviceMap dm = new udtDeviceMap(); dm.devname = devname; //dm.dommap.type_g[]= AllTag[j]; dm.domh - 1] = dm; }
二.自定义编辑器
`[MenuItem(“MyMenu/getAllTag”)]
public static void getAllTag() { WriteFile write = new WriteFile(); write.DeleteFile(Application.persistentDataPath, scenename + ".js"); write.CreateFile(Application.persistentDataPath, scenename+".js", sDeviceMap); print("当前文件路径:" + Application.persistentDataPath); }``
三.写本地Json
这里用到了一个Newtonsoft.Json的DLL来转换Json,注意版本最好使用2.0.
- `public void CreateFile(string path, string name, string info) { //文件流信息 StreamWriter sw; FileInfo t = new FileInfo(path + "//" + name); if (!t.Exists) { //如果此文件不存在则创建 sw = t.CreateText(); } else { //如果此文件存在则打开 sw = t.AppendText(); } //以行的形式写入信息 sw.WriteLine(info); //关闭流 sw.Close(); //销毁流 sw.Dispose(); } /** * 读取文本文件 * path:读取文件的路径 * name:读取文件的名称 */ public ArrayList LoadFile(string path, string name) { //使用流的形式读取 StreamReader sr = null; try { sr = File.OpenText(path + "//" + name); } catch (Exception e) { //路径与名称未找到文件则直接返回空 return null; } string line; ArrayList arrlist = new ArrayList(); while ((line = sr.ReadLine()) != null) { //一行一行的读取 //将每一行的内容存入数组链表容器中 arrlist.Add(line); } //关闭流 sr.Close(); //销毁流 sr.Dispose(); //将数组链表容器返回 return arrlist; } `
相关文章推荐
- 遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值
- 对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值
- 获取JavaScript自定义对象的所有属性和值
- jqplot AJAX 获取JSON对象 动态生成柱状图
- 使用java调用C语言的方法,让java获取串口数据并生成一个String对象的代码。
- [cocos2dx 3.0 (四)] CocoStudio生成Json文件 + 使用rapidjson获取Json数据
- Js 怎么遍历json对象所有key及根据动态key获取值
- C# 把数据集生成为JSON格式 并在使用Jquery 获取JSON数据
- Cocos Studio生成Json文件并使用rapidjson获取Json数据
- json对象,使用 “ . ”获取值是,不能使用变量作为属性名。
- 开发android4.0以上版本网络编程模块的问题 使用URLConnection 怎么连接不上 获取Json字符串
- XE7 & IOS开发之开发账号(2):发布证书、发布授权profile的申请使用,附Ad hoc真机调试、生成ipa文件演示(XCode所有版本通用,有图有真相)
- 使用dojo declare(继承v102版本) 时出现无法根据id获取到对象的问题
- Js 怎么遍历json对象所有key及根据动态key获取值
- cocos2dx 代码使用cocoStudio 生成的场景json文件
- 在使用Sql server 2005 生成脚本时,出现目标服务器版本不支持该对象或不支持该对象的一个属性。 (Microsoft.SqlServer.Smo)的解决办法
- 使用BeanPostProcessor接口对受管Bean进行预处理,通过自定义Annotation,获取所有Controller的Url
- 百度编辑器UEEDITOR使用简单介绍 UEditor表单提交和后台交互详解 最后更新对应的版本:1.2.5.1 教程描述: 富文本编辑器的使用开发中,表单提交有多种场景,编辑器初始化有新增文章和编辑
- 使用request生成一个枚举类获取所有的<input>的内容
- 使用JPublisher生成Oracle自定义类型的Java对象