Tools(一)——BuildMethod

来源:互联网 发布:女生相机软件 编辑:程序博客网 时间:2024/06/05 02:50
Tools这几篇都是写的都是这个目录下的文件BuildMethod.cs文件目录为Assets/Scripts/Tools/Editor/BuildMethod.cs

因为都写好注释了,下面是所有代码:

using UnityEngine;using UnityEditor;using System.IO;using System.Collections;    public class BuildMethod    {        //public static string getNameByPath(string path)        //{        //    String[] words = path.Split(new char[] { '/','\\' });        //    if (words != null && words.Length != 0)        //    {        //        String[] splitstr = words[words.Length - 1].Split(new char[] { '.' });        //        if (splitstr != null && splitstr.Length != 0)        //        {        //            String name = splitstr[0];        //            return name;        //        }        //    }        //    return "";        //}        //这函数很有问题,下面完全是越界了。        //path指的是在磁盘上的地址:Application.dataPath + /Resources/Version.bytes这样子的        //这个函数实际上是要返回资源相对于Resources文件夹的路径        //例如:传入的path为Resources/prefab/test.prefab,        //这个时候下面循环中的count + 3完全越界了,        //所以这函数有问题        //但是也有可能在用的地方有特殊情况        //所以我在下面重新写了一个函数,这个函数不修改        public static string getUnityPathInResourceFolder(string path)        {            //根据正斜杠和反斜杠把字符串分开成多个字符串            string[] rootwords = Application.dataPath.Split(new char[] { '/', '\\' });            int count = rootwords.Length;            string[] words = path.Split(new char[] { '/', '\\' });            if (words != null)            {                //我测试过,这里count + 2 很有可能会越界了,编译都通不过的                string rtvalue = words[count + 2];                for (int i = count + 3; i < words.Length; ++i)                {                    rtvalue += ("/" + words[i]);                }                return rtvalue;            }            return "";        }        /*        public static string getUnityPathInResourceFolder(string path)        {            string[] rootwords = Application.dataPath.Split(new char[] { '/', '\\' });            int count = rootwords.Length;            string[] words = path.Split(new char[] { '/', '\\' });            if (words != null)            {                string rtvalue = words[count + 1];                if ((count + 2) < words.Length)                {                    for (int i = count + 2; i < words.Length; i++)                    {                        rtvalue += ("/" + words[i]);                    }                }                return rtvalue;            }            return "";        }        */        //看嘛,这下面这函数就完全正常了        //例如:path = Applicaition.dataPath + "/Resources/Version.bytes"        //返回的就会是 Assets/Resources/Version.bytes        //不过感觉也可以用另一种方法来拼接这个的返回值        public static string getUnityPath(string path)        {            string[] rootwords = Application.dataPath.Split(new char[] { '/', '\\' });            int count = rootwords.Length;            string[] words = path.Split(new char[] { '/', '\\' });            if (words != null)            {                string rtvalue = words[count - 1];                for (int i = count; i < words.Length; ++i)                {                    rtvalue += ("/" + words[i]);                }                return rtvalue;            }            return "";        }        //这函数把路径中的正斜杠反斜杠都换成下划线了,也去掉了空格.        //但是暂时还不知道该怎么样        public static string getShaderName(string sName)        {            //for (int i = 0; i < sName.Length; ++i)            //{            //    if (sName[i] == '/' && sName[i] == '\\')            //    {            //        sName. = '_';            //    }            //}            string[] words = sName.Split(new char[] {' ','/', '\\' });            string retstr = words[0];            for (int i = 1; i < words.Length; ++i)            {                retstr += ('_' + words[i]);            }            return retstr;        }    }