Unity工具是游戏制做必须配备的,Unity虽然提供了强大的编辑器,可是对于游戏开发的需求来讲仍是远远不够的,这也须要咱们本身编写一些小工具去实现特定的需求,好比编写遍历资源文件的全部子文件用于打包处理,这些须要咱们本身去封装函数接口。json
private static List<string> GetResAllDirPath()
{
List<string> ret = AssetBundleBuild.GetAllLocalSubDirs(cAssetsResourcesPath);
if (DirExistResource(cAssetsResourcesPath))
{
if (ret == null)
ret = new List<string>();
ret.Add(cAssetsResourcesPath);
}
if (ret != null)
ret.Sort(OnDirSort);
return ret;
}
复制代码
该函数是对外提供的函数,它返回资源的全部文件以及子文件,在该函数中使用了接口bash
AssetBundleBuild.GetAllLocalSubDirs(cAssetsResourcesPath);
复制代码
public static List<string> GetAllLocalSubDirs(string rootPath)
{
if (string.IsNullOrEmpty (rootPath))
return null;
string fullRootPath = System.IO.Path.GetFullPath (rootPath);
if (string.IsNullOrEmpty (fullRootPath))
return null;
string[] dirs = System.IO.Directory.GetDirectories (fullRootPath);
if ((dirs == null) || (dirs.Length <= 0))
return null;
List<string> ret = new List<string> ();
for (int i = 0; i < dirs.Length; ++i) {
string dir = AssetBunbleInfo.GetLocalPath(dirs[i]);
ret.Add (dir);
}
for (int i = 0; i < dirs.Length; ++i) {
string dir = dirs[i];
List<string> list = GetAllLocalSubDirs(dir);
if (list != null)
ret.AddRange(list);
}
return ret;
}
复制代码
在上述函数中调用了函数接口编辑器
AssetBunbleInfo.GetLocalPath(dirs[i]);
复制代码
该接口的实现函数以下所示:函数
// 得到根据Assets目录的局部目录
public static string GetLocalPath(string path)
{
return AssetBundleMgr.GetAssetRelativePath(path);
}
复制代码
继续接口的调用函数工具
GetAssetRelativePath
复制代码
实现以下所示:ui
public static string GetAssetRelativePath(string fullPath)
{
if (string.IsNullOrEmpty(fullPath))
return string.Empty;
fullPath = fullPath.Replace("\\", "/");
int index = fullPath.IndexOf("Assets/", StringComparison.CurrentCultureIgnoreCase);
if (index < 0)
return fullPath;
string ret = fullPath.Substring(index);
return ret;
}
复制代码
最后一个函数是DirExistResource函数用于判断文件是否存在,它的实现代码以下所示: // 根据目录判断是否有资源文件spa
public static bool DirExistResource(string path)
{
if (string.IsNullOrEmpty (path))
return false;
string fullPath = Path.GetFullPath (path);
if (string.IsNullOrEmpty (fullPath))
return false;
string[] files = System.IO.Directory.GetFiles (fullPath);
if ((files == null) || (files.Length <= 0))
return false;
for (int i = 0; i < files.Length; ++i) {
string ext = System.IO.Path.GetExtension(files[i]);
if (string.IsNullOrEmpty(ext))
continue;
for (int j = 0; j < ResourceExts.Length; ++j)
{
if (string.Compare(ext, ResourceExts[j], true) == 0)
{
if ((ResourceExts[j] == ".fbx") || (ResourceExts[j] == ".controller"))
{
// ingore xxx@idle.fbx
string name = Path.GetFileNameWithoutExtension(files[i]);
if (name.IndexOf('@') >= 0)
return false;
} else
if (ResourceExts[j] == ".unity")
{
if (!IsVaildSceneResource(files[i]))
return false;
}
return true;
}
}
}
return false;
}
复制代码
代码中的code
ResourceExts
复制代码
是事先定义好的可以遍历到的文件扩展名,以下所示: // 支持的资源文件格式xml
private static readonly string[] ResourceExts = {".prefab", ".fbx",
".png", ".jpg", ".dds", ".gif", ".psd", ".tga", ".bmp",
".txt", ".bytes", ".xml", ".csv", ".json",
".controller", ".shader", ".anim", ".unity", ".mat",
".wav", ".mp3", ".ogg",
".ttf",
".shadervariants", ".asset"};
复制代码
最后一步是判断scene是否有效的函数接口:接口
IsVaildSceneResource
复制代码
实现代码以下所示:
private static bool IsVaildSceneResource(string fileName)
{
bool ret = false;
if (string.IsNullOrEmpty (fileName))
return ret;
string localFileName = AssetBunbleInfo.GetLocalPath (fileName);
if (string.IsNullOrEmpty (localFileName))
return ret;
var scenes = EditorBuildSettings.scenes;
if (scenes == null)
return ret;
var iter = scenes.GetEnumerator ();
while (iter.MoveNext()) {
EditorBuildSettingsScene scene = iter.Current as EditorBuildSettingsScene;
if ((scene != null) && scene.enabled)
{
if (string.Compare(scene.path, localFileName, true) == 0)
{
ret = true;
break;
}
}
}
return ret;
}
复制代码
这样遍历全部资源下的文件和子文件的全部函数就完成了,放在工具代码中实现便可。