private static void ProduceAssetBundleConfig() { ABConfig abConfig = new ABConfig(); abConfig.directoryABPathList.Add(new ABConfig.DirectoryAB() { ABName = "assetbundleconfig", DirectoryPath = "Assets/Framework/AssetBundle/Config" }); UnityEditor.AssetDatabase.CreateAsset(abConfig, GlobalConfigData.AssetBundleConfigPath); UnityEditor.AssetDatabase.SaveAssets(); UnityEditor.AssetDatabase.Refresh(); }
private static void BuildAssetBundle() { m_directoryABDict.Clear(); m_prefabABDict.Clear(); m_buildedFiles.Clear(); m_loadFiles.Clear(); //读取打包策略对应的配置文件 ABConfig abConfig = AssetDatabase.LoadAssetAtPath <ABConfig>(GlobalConfigData.AssetBundleConfigPath); //获取整体目录打包的文件夹 foreach (ABConfig.DirectoryAB item in abConfig.directoryABPathList) { if (m_directoryABDict.ContainsKey(item.ABName)) { Debug.LogError("当前文件夹: " + item.DirectoryPath + "的AB包名字重复, 请检查!"); } else { m_directoryABDict.Add(item.ABName, item.DirectoryPath); m_buildedFiles.Add(item.DirectoryPath); //文件夹是会被打包的,所以需要加入 m_loadFiles.Add(item.DirectoryPath); //文件夹中的资源是可能被动态加载的 } } //获取需要打包的Prefab的文件路径 string[] allPrefabGUIDAry = AssetDatabase.FindAssets("t:Prefab", abConfig.prefabABPathList.ToArray()); foreach (string guid in allPrefabGUIDAry) { string path = AssetDatabase.GUIDToAssetPath(guid); m_loadFiles.Add(path); //Prefab预制件是一定会被动态加载的 GameObject go = AssetDatabase.LoadAssetAtPath <GameObject>(path); string[] goDependencies = AssetDatabase.GetDependencies(path); //得到某个路径下资源需要依赖的所有文件全路径,包括资源自身的路径在内 List <string> goDependenciesPathList = new List <string>(); for (int i = 0; i < goDependencies.Length; i++) { if (goDependencies[i].EndsWith(".cs") == false && BuildedFilesContainFile(goDependencies[i]) == false) //过滤掉.cs脚本和已经被标记打包的文件 { goDependenciesPathList.Add(goDependencies[i]); m_buildedFiles.Add(goDependencies[i]); } } if (m_prefabABDict.ContainsKey(go.name)) { Debug.LogError("当前Prefab: " + go.name + "的AB包名字重复, 请检查!"); } else { m_prefabABDict.Add(go.name, goDependenciesPathList); } } //对所有的整体文件夹设置AssetBundleName foreach (var item in m_directoryABDict) { SetAssetBundleName(item.Key, item.Value); } //对所有的指定文件中的Prefab设置AssetBundleName foreach (var item in m_prefabABDict) { foreach (string filePath in item.Value) { SetAssetBundleName(item.Key, filePath); } } //制作依赖关系 MakeDependenceRelationShip(); //开始打包 BuildPipeline.BuildAssetBundles(GlobalConfigData.AssetBundleBuildTargetPath, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget); //打包成功后, 需要取消项目中的AssetBundleName的标记 string[] assetBundleNameAry = AssetDatabase.GetAllAssetBundleNames(); foreach (string assetBundleName in assetBundleNameAry) { AssetDatabase.RemoveAssetBundleName(assetBundleName, true); } //刷新Project面板 AssetDatabase.Refresh(); Debug.Log("打包成功..."); }