【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