Unity遍歷資源下的全部文件以及子文件

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;  
    }  
複製代碼

這樣遍歷全部資源下的文件和子文件的全部函數就完成了,放在工具代碼中實現便可。

相關文章
相關標籤/搜索