Unty5.x裡Assetbundle的自動標記和打包
阿新 • • 發佈:2018-11-19
最近研究了一下assetbundle的自動標記和打包,參考了很多網上教程寫了一篇程式碼,廢話不多說,直接看程式碼把
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; public class AssetBundleEditor { #region 自動做標記 //思路 //1.找到資源儲存的資料夾 //2.遍歷裡面每個場景資料夾 //3.遍歷場景資料夾裡的所有檔案系統 //4.如果訪問的是資料夾:繼續訪問裡面所有的檔案系統,直到找到 檔案(遞迴) //5.找到檔案,修改他的 assetbundle labels //6.用 AssetImporter 類 修改名稱和字尾 //7.儲存對應的資料夾名和具體路徑 [MenuItem("AssetBundle/Set AssetBundle Labels")] public static void SetAssetBundleLabels() { //移除所有沒有使用的標記 AssetDatabase.RemoveUnusedAssetBundleNames(); //1.找到資源儲存的資料夾 string assetDirectory = Application.dataPath + "/Res";//"D:/WorkSpace/UnityWorkSpace/MobaClient/AssetBundles/Assets/Res"; DirectoryInfo directoryInfo = new DirectoryInfo(assetDirectory); DirectoryInfo[] sceneDirectories = directoryInfo.GetDirectories(); //2.遍歷裡面每個場景資料夾 foreach (DirectoryInfo tempDirectoryInfo in sceneDirectories) { string sceneDirectory = assetDirectory + "/" + tempDirectoryInfo.Name; DirectoryInfo sceneDirectoryInfo = new DirectoryInfo(sceneDirectory); //錯誤檢測 if (sceneDirectoryInfo == null) { Debug.LogError(sceneDirectory + "不存在"); return; } else { Dictionary<string, string> namePathDict = new Dictionary<string, string>(); //3.遍歷場景資料夾裡的所有檔案系統 //D:\WorkSpace\UnityWorkSpace\MobaClient\AssetBundles\Assets\Res //D:/WorkSpace/UnityWorkSpace/MobaClient/AssetBundles/Assets/Res int index = sceneDirectory.LastIndexOf("/"); string sceneName = sceneDirectory.Substring(index + 1); onSceneFileSystemInfo(sceneDirectoryInfo, sceneName, namePathDict); onWriteConfig(sceneName, namePathDict); } }//end foreach AssetDatabase.Refresh(); Debug.LogWarning("設定成功"); }//end set /// <summary> /// 記錄配置檔案 /// </summary> private static void onWriteConfig(string sceneName, Dictionary<string, string> namePathDict) { string path = PathUtil.GetAssetBundleOutPath()+ "/" + sceneName + "Record.txt"; using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(namePathDict.Count); foreach (KeyValuePair<string, string> kv in namePathDict) sw.WriteLine(kv.Key + " " + kv.Value); } } } private static void onSceneFileSystemInfo(FileSystemInfo fileSystemInfo, string sceneName, Dictionary<string, string> namePathDict) { if (!fileSystemInfo.Exists) { Debug.LogError(fileSystemInfo.FullName + "不存在"); return; } DirectoryInfo directoryInfo = fileSystemInfo as DirectoryInfo; FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos(); foreach (var tempfileInfo in fileSystemInfos) { FileInfo fileInfo = tempfileInfo as FileInfo; if (fileInfo == null) { //代表強轉失敗,不是檔案 就是資料夾 //4.如果訪問的是資料夾:繼續訪問裡面所有的檔案系統,直到找到 檔案(遞迴) onSceneFileSystemInfo(tempfileInfo, sceneName, namePathDict); } else { //檔案 //5.找到檔案,修改他的 assetbundle labels setLables(fileInfo, sceneName, namePathDict); } } } /// <summary> /// 修改資原始檔的 assetbundle labels /// </summary> /// <param name="fileInfo"></param> /// <param name="sceneName"></param> private static void setLables(FileInfo fileInfo, string sceneName, Dictionary<string, string> namePathDict) { //忽視unity自身生成的meta檔案 if (fileInfo.Extension == ".meta") return; string bundleName = getBundleName(fileInfo, sceneName); int index = fileInfo.FullName.IndexOf("Assets"); string assetPath = fileInfo.FullName.Substring(index); //6.用 AssetImporter 類 修改名稱和字尾 AssetImporter assetImporter = AssetImporter.GetAtPath(assetPath); assetImporter.assetBundleName = bundleName; if (fileInfo.Extension == ".unity") assetImporter.assetBundleVariant = "u3d"; else assetImporter.assetBundleVariant = "assetbundle"; //新增到字典 string folderName = ""; if (bundleName.Contains("/")) folderName = bundleName.Split('/')[1]; else folderName = bundleName; string bundlePath = assetImporter.assetBundleName + "." + assetImporter.assetBundleVariant; if (!namePathDict.ContainsKey(folderName)) namePathDict.Add(folderName, bundlePath); } /// <summary> /// 獲取包名 /// </summary> private static string getBundleName(FileInfo fileInfo, string sceneName) { string windowPath = fileInfo.FullName; string unityPath = windowPath.Replace(@"\", "/"); int Index = unityPath.IndexOf(sceneName) + sceneName.Length; string bundlePath = unityPath.Substring(Index + 1); if (bundlePath.Contains("/")) { string[] temp = bundlePath.Split('/'); return sceneName + "/" + temp[0]; } else { return sceneName; } } #endregion #region 打包 [MenuItem("AssetBundle/Build AssetBundles")] static void BuildAssetBundles() { string outPath = PathUtil.GetAssetBundleOutPath(); BuildPipeline.BuildAssetBundles(outPath, 0, BuildTarget.StandaloneWindows64); } #endregion #region 刪除 [MenuItem("AssetBundle/Delete All")] static void DeleteAssetBundle() { string outPath = PathUtil.GetAssetBundleOutPath(); Directory.Delete(outPath, true); File.Delete(outPath + ".meta"); AssetDatabase.Refresh(); } #endregion }
還有一篇獲取路徑的
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; /// <summary> /// 路徑 /// </summary> public class PathUtil { /// <summary> /// 獲取assetbundle的輸出目錄 /// </summary> /// <returns></returns> public static string GetAssetBundleOutPath() { string outPath = getPlatformPath() + "/" + getPlatformName(); if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath); return outPath; } /// <summary> /// 自動獲取對應平臺的路徑 /// </summary> /// <returns></returns> private static string getPlatformPath() { switch (Application.platform) { case RuntimePlatform.WindowsPlayer: case RuntimePlatform.WindowsEditor: return Application.streamingAssetsPath; case RuntimePlatform.Android: return Application.persistentDataPath; default: return null; } } /// <summary> /// 獲取對應平臺名字 /// </summary> /// <returns></returns> private static string getPlatformName() { switch (Application.platform) { case RuntimePlatform.WindowsPlayer: case RuntimePlatform.WindowsEditor: return "Windows"; case RuntimePlatform.Android: return "Android"; default: return null; } } /// <summary> /// 獲取www協議的路徑 /// </summary> public static string GetWWWPath() { switch (Application.platform) { case RuntimePlatform.WindowsPlayer: case RuntimePlatform.WindowsEditor: return "file:///"+ GetAssetBundleOutPath(); case RuntimePlatform.Android: return "jar:file://" + GetAssetBundleOutPath(); default: return null; } } }