获取Assets文件夹同级的目录
我们要读取Assets目录同级的文件夹Assets.diff
, 该文件夹的全路径可以这样获得:
//方法一
DirectoryInfo topDir = Directory.GetParent(Application.dataPath);
string diffFilePath = topDir.FullName + "/" + "Assets.diff";
//方法二
string diffFilePath = GetParentDirectory(Application.dataPath, 1) + "/" + "Assets.diff";
其中diffFilePath
即为全路径
GetParentDirectory
方法可以获得某路径的n级父目录
public static string GetParentDirectory(string path, int parentCount)
{
int index = path.Length;
while (parentCount > 0 && index > 0)
{
index = path.LastIndexOf('/', index - 1, StringComparison.Ordinal);
--parentCount;
}
if (index < 0){
return null;
}
return path.Substring(0, index);
}
使用Editor进程条
当我们执行时间比较久的进程时(例如遍历Assets目录下所有FBX资源),可以使用EditorUtility 中的进度条来展示进程执行到哪一步了.
具体代码记录如下:
//展示进度条
EditorUtility.DisplayProgressBar("正在检查", "正在获取文件列表...", 0.0f);
var info = string.Format("{0}/{1}", tempCount, allCount);
EditorUtility.DisplayProgressBar("正在检查", "已检查" + info, (float)tempCount / allCount);
//最后一定要记得清理掉进度条, 不然它就一直留在界面上啦
EditorUtility.ClearProgressBar();