【Unity3D5.6版本使用(1)】自定义编辑器获取场景所有对象Tag生成Json
来源:互联网 发布:ubuntu 17.10 安装wine 编辑:程序博客网 时间:2024/06/05 16:42
在unity3D5.x版本中,一部分以前的代码无法正常运行,比如:加载场景时,使用Application.loadedLevel会提示“已过时”,需要使用EditorSceneManager.OpenScene(FileName[scount]);所以写一篇记录下在新版本环境实现一些功能的笔记(一些没变化)。
一.获取当前场景所有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; }`
阅读全文
0 0
- 【Unity3D5.6版本使用(1)】自定义编辑器获取场景所有对象Tag生成Json
- Unity编辑器获取文件夹下所有对象
- cocos2dx 代码使用cocoStudio 生成的场景json文件
- cocos2dx 代码使用cocoStudio 生成的场景json文件
- Highcharts axja 获取json对象动态生成报表生成
- Highcharts ajax获取json对象动态生成报表生成 .
- JavaScript遍历json对象 (包括所有key及根据动态key获取值)
- unity3d5.1物体椭圆旋转选择界面实现(一)
- unity3d5.1物体椭圆旋转选择界面实现(二)
- 列出场景中所有对象
- 遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值
- 导出Unity场景的所有游戏对象信息,一种是XML一种是JSON
- 使用json.net 组件 生成JSON字符串,自定义日期格式
- 关于Unity3D5.X 动态批处理(Dynamic Batching)实测,区别于4.0版本
- 获取JavaScript自定义对象的所有属性和值
- 自定义标签(tag)
- jqplot AJAX 获取JSON对象 动态生成柱状图
- handlebars获取json数据(集合对象)及各种自定义helper数据格式化工具
- centos7安装kvm虚拟机
- 2017年华为优招机试题_括号匹配_编程题
- hadoop32
- PTN设备标准规范
- C#常用控件属性及方法介绍
- 【Unity3D5.6版本使用(1)】自定义编辑器获取场景所有对象Tag生成Json
- python的社会形态模拟
- 独木舟上的旅行
- Dijkstr 算法 求单源最小路径
- docker--docker入门理论
- 安卓中为了获取context的方法和区别(getContext,getActivity,this,mainActivity.this等)
- python-opencv眼睛检测,笑脸检测
- 2、MYSQL
- swift_043(Swift 懒加载(lazy) )