BuildPipeline.BuildAssetBundles() 這個函數,有多個重載,一個不用AssetBundleBuild數組,一個需要,如果設置了AssetLabels,那么這時候是不需要的,如果沒有設置,那么我們就可以打包自定義項,
AssetBundleBuild assetBundleBuild = new AssetBundleBuild(); 我們可以new 出來一個 AssetBundleBuild ,在這里指定他的 assetBundleName 和 assetBundleVariant ,也就是前面提到的AssetLabels,
同時還要指出此資源所在地址 assetBundleBuild.assetNames = new string[] { path }; 並將所有的 AssetBundleBuild 做為數組返回,填入 BuildPipeline.BuildAssetBundles() 所需要的參數中;
詳見代碼
static List<AssetBundleBuild> listassets = new List<AssetBundleBuild>(); //第二種打包方式用 static List<DirectoryInfo> listfileinfo = new List<DirectoryInfo>(); static bool isover = false; //是否檢查完成,可以打包 public static bool GetState() { return isover; } public static AssetBundleBuild[] GetAssetBundleBuilds() { return listassets.ToArray(); } public static void SetAssetBundleBuilds() { UnityEngine.Object obj = Selection.activeObject; //選中的物體 string path = AssetDatabase.GetAssetPath(obj);//選中的文件夾 //目錄結構 Assets/Resources 沒有Application.datapath SearchFileAssetBundleBuild(path); } public static void SearchFileAssetBundleBuild(string path) //是文件,繼續向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); listfileinfo.Clear(); foreach (var item in fileSystemInfos) { int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if ((item as DirectoryInfo) != null) listfileinfo.Add(item as DirectoryInfo); if (!name.Contains(".meta")) { CheckFileOrDirectoryReturnBundleName(item, path + "/" + name); } } if (listfileinfo.Count == 0) isover = true; else { Debug.LogError(listfileinfo.Count); } } public static string CheckFileOrDirectoryReturnBundleName(FileSystemInfo fileSystemInfo, string path) //判斷是文件還是文件夾 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { string[] strs = path.Split('.'); string[] dictors = strs[0].Split('/'); string name = ""; for (int i = 1; i < dictors.Length; i++) { if (i < dictors.Length - 1) { name += dictors[i] + "/"; } else { name += dictors[i]; } } AssetBundleBuild assetBundleBuild = new AssetBundleBuild(); assetBundleBuild.assetBundleName = name; assetBundleBuild.assetBundleVariant = "bytes"; assetBundleBuild.assetNames = new string[] { path }; listassets.Add(assetBundleBuild); return name; } else { SearchFileAssetBundleBuild(path); return null; } }
然后在這里調用
注意,因為文件夾下還可能有文件夾,我們要等所有資源遍歷完成,再打包,所以用了一個死循環
[MenuItem("Tools/打包選中文件夾下所有項目")] public static void BundSelectionAssets() { BuilderAssetsBunlds.SetAssetBundleBuilds(); while (true) { if (BuilderAssetsBunlds.GetState()) break; else Debug.LogError("等待....."); } BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuilderAssetsBunlds.GetAssetBundleBuilds(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); }
好了,今天就到這里,加上前面的,所有的都在這里
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using System.Text; using System; using System.Security.Cryptography; public class BuilderAssetsBunlds { public static string GetOutAssetsDirecotion() //打包輸出地址,后續會再完善 { string assetBundleDirectory = Application.streamingAssetsPath; if (!Directory.Exists(assetBundleDirectory)) { Directory.CreateDirectory(assetBundleDirectory); } return assetBundleDirectory; } /// <summary> /// 檢查目標文件下的文件系統 /// </summary> public static void CheckFileSystemInfo() //檢查目標目錄下的文件系統 { AssetDatabase.RemoveUnusedAssetBundleNames(); //移除沒有用的assetbundlename UnityEngine.Object obj = Selection.activeObject; //選中的物體 string path = AssetDatabase.GetAssetPath(obj);//選中的文件夾 //目錄結構 Assets/Resources 沒有Application.datapath CoutineCheck(path); } public static void CheckFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判斷是文件還是文件夾 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { SetBundleName(path); } else { CoutineCheck(path); } } public static void CoutineCheck(string path) //是文件,繼續向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); foreach (var item in fileSystemInfos) { // Debug.Log(item); int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if (!name.Contains(".meta")) { CheckFileOrDirectory(item, path + "/" + name); //item 文件系統,加相對路徑 } } } public static void SetBundleName(string path) //設置assetbundle名字 { // Debug.LogError(path); var importer = AssetImporter.GetAtPath(path); string[] strs = path.Split('.'); string[] dictors = strs[0].Split('/'); string name = ""; for (int i = 1; i < dictors.Length; i++) { if (i < dictors.Length - 1) { name += dictors[i] + "/"; } else { name += dictors[i]; } } if (importer != null) { importer.assetBundleName = name; importer.assetBundleVariant = "bytes"; // importer.assetBundleName = GetGUID(path); //兩種設置方式 } else Debug.Log("importer是空的"); } public static string GetGUID(string path) { return AssetDatabase.AssetPathToGUID(path); } //*****************配置文件設置***********************//////////// static Dictionary<string, string> dicversoion = new Dictionary<string, string>(); //版本 static Dictionary<string, string> dicurl = new Dictionary<string, string>(); //下載地址 public static string GetAssetBundleStringPath() //得到存放打包資源的文件路徑 { string path = Application.streamingAssetsPath; //Debug.Log(path); string[] strs = path.Split('/'); int idx = 0; for (int i = 0; i < strs.Length; i++) { if (strs[i] == "Assets") idx = i; } // Debug.Log(idx); //path = strs[strs.Length - 2] + "/" + strs[strs.Length - 1]; string str = ""; for (int i = idx; i < strs.Length; i++) { if (i != strs.Length - 1) str += strs[i] + "/"; else if (i == strs.Length - 1) str += strs[i]; //Debug.Log(i); } path = str; return path; //Debug.Log(path); } public static void CheckAssetBundleDir(string path) //是文件,繼續向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); foreach (var item in fileSystemInfos) { // Debug.Log(item); int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if (!name.Contains(".meta")) { CheckAssetBundleFileOrDirectory(item, path + "/" + name); //item 文件系統,加相對路徑 } } } public static void CheckAssetBundleFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判斷是文件還是文件夾 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { Debug.Log("不為空,MD5值==>" + GetMD5(path)); // string guid = AssetDatabase.AssetPathToGUID(path); // Debug.Log("不為空,文件名字是==>>"+guid); Addconfigdic(fileInfo.Name, GetMD5(path)); } else { CheckAssetBundleDir(path); } } public static string GetMD5(string path) { FileStream fs = new FileStream(path, FileMode.Open); MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(fs); fs.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } public static void Addconfigdic(string name, string verMd5) { dicversoion.Add(name, verMd5); } public static void CreateConfigFile(string path) //創建配置文件 { } // -------------------------------我是分割線------------------------------------ // 割............................. static List<AssetBundleBuild> listassets = new List<AssetBundleBuild>(); //第二種打包方式用 static List<DirectoryInfo> listfileinfo = new List<DirectoryInfo>(); static bool isover = false; //是否檢查完成,可以打包 public static bool GetState() { return isover; } public static AssetBundleBuild[] GetAssetBundleBuilds() { return listassets.ToArray(); } public static void SetAssetBundleBuilds() { UnityEngine.Object obj = Selection.activeObject; //選中的物體 string path = AssetDatabase.GetAssetPath(obj);//選中的文件夾 //目錄結構 Assets/Resources 沒有Application.datapath SearchFileAssetBundleBuild(path); } public static void SearchFileAssetBundleBuild(string path) //是文件,繼續向下 { DirectoryInfo directory = new DirectoryInfo(@path); FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos(); listfileinfo.Clear(); foreach (var item in fileSystemInfos) { int idx = item.ToString().LastIndexOf(@"\"); string name = item.ToString().Substring(idx + 1); if ((item as DirectoryInfo) != null) listfileinfo.Add(item as DirectoryInfo); if (!name.Contains(".meta")) { CheckFileOrDirectoryReturnBundleName(item, path + "/" + name); } } if (listfileinfo.Count == 0) isover = true; else { Debug.LogError(listfileinfo.Count); } } public static string CheckFileOrDirectoryReturnBundleName(FileSystemInfo fileSystemInfo, string path) //判斷是文件還是文件夾 { FileInfo fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { string[] strs = path.Split('.'); string[] dictors = strs[0].Split('/'); string name = ""; for (int i = 1; i < dictors.Length; i++) { if (i < dictors.Length - 1) { name += dictors[i] + "/"; } else { name += dictors[i]; } } AssetBundleBuild assetBundleBuild = new AssetBundleBuild(); assetBundleBuild.assetBundleName = name; assetBundleBuild.assetBundleVariant = "bytes"; assetBundleBuild.assetNames = new string[] { path }; listassets.Add(assetBundleBuild); return name; } else { SearchFileAssetBundleBuild(path); return null; } } }
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; public class Constant { public static string SD_DIR = "SD_DIR"; } public class BundleSystem { //打包、、、、、、、、、、、、、、、、、、、、、、 [MenuItem("Tools/打包所有設置AssetLable項目")] public static void BundlerAssets() { // Debug.LogError("打包Assets"); BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } [MenuItem("Tools/打包選中文件夾下所有項目")] public static void BundSelectionAssets() { BuilderAssetsBunlds.SetAssetBundleBuilds(); while (true) { if (BuilderAssetsBunlds.GetState()) break; else Debug.LogError("等待....."); } BuildPipeline.BuildAssetBundles(BuilderAssetsBunlds.GetOutAssetsDirecotion(), BuilderAssetsBunlds.GetAssetBundleBuilds(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows); } [MenuItem("Tools/設置Assetbundle名字")] public static void SetAssetBundellabls() { BuilderAssetsBunlds.CheckFileSystemInfo(); } [MenuItem("Tools/設置配置文件")] public static void SetAssetConfig() { BuilderAssetsBunlds.CheckAssetBundleDir(BuilderAssetsBunlds.GetAssetBundleStringPath()); } [MenuItem("Tools/測試")] static void PackAsset() { Debug.Log("Make AssetsBundle"); // List<AssetBundleBuild> builds = new List<AssetBundleBuild>(); AssetBundleBuild build = new AssetBundleBuild(); build.assetBundleName = "first"; build.assetBundleVariant = "u3"; // build.assetNames[0] = "Assets/Resources/mascot.prefab"; build.assetNames = new string[] { "Assets/PNG/結算副本.png" }; builds.Add(build); // 第一個參數為打包文件的輸出路徑,第二個參數為打包資源的列表,第三個參數為打包需要的操作,第四個為打包的輸出的環境 //BuildPipeline.BuildAssetBundles(@"Assets/Bundle", builds.ToArray(), // BuildAssetBundleOptions.None, BuildTarget.Android); BuildPipeline.BuildAssetBundles(@"Assets/Bundle", builds.ToArray(), BuildAssetBundleOptions.None, BuildTarget.Android); Debug.Log("11111" + builds.ToArray() + "222222"); } }
上篇傳送門
http://www.cnblogs.com/lzy575566/p/7714510.html