public static void AddPushDep(CollectedDepAssetInfo info, bool forceBuild) { // Library类型Asset,没有路径,所有使用自定义的BuildAssetPath string assetPath; bool needBuild = true; if (info.UnityAssetType == UnityAssetType.Object) { assetPath = info.UnityAssetPath; needBuild = forceBuild || CheckNeedBuildAsset(info.UnityAssetType, assetPath); // 下面告诉要强制build,或在文件改变时才真的进行Build } else { assetPath = info.BuildAssetPath; needBuild = forceBuild || CheckNeedBuildAsset(info.UnityAssetType, assetPath); // 其实基本Library资源是肯定要打包的,这一句其实可以忽略 } if (!needBuild) { return; } var buildAssetPath = info.BuildAssetPath; var depObjectsMap = CollectAndPushBuildDependencies(info.Asset, needBuild); BuildPipeline.PushAssetDependencies(); BuildAssetBundle(info.Asset, buildAssetPath, GetBuildAssetPaths(depObjectsMap)); DependencyPool.Add(buildAssetPath); }
/// <summary> /// 智能收集依赖,剔除非用于AssetBundle打包的部分,返回路径list(路径去掉了'Assets/') /// </summary> /// <param name="buildObj"></param> /// <returns></returns> private static List <CollectedDepAssetInfo> CollectDependenciesPaths(Object buildObj) { var assetPath = AssetDatabase.GetAssetPath(buildObj); var depObjects = EditorUtility.CollectDependencies(new[] { buildObj }); // 使用Dict,去掉重复 var depObjectsMap = new Dictionary <string, CollectedDepAssetInfo>(); foreach (var depObj in depObjects) { if (depObj == null) { Log.Error("Found NULL obj when collect dep from '{0}'", buildObj); continue; } var buildAssetPath = GetBuildAssetPath(depObj); // 过滤 var depExtType = GetAssetExtType(depObj); // 某些类型进行忽略 if (Define.IgnoreDepType.Contains(depExtType)) { continue; } // 很多跟自己路径一样的 var depAssetPath = AssetDatabase.GetAssetPath(depObj); if (depAssetPath == assetPath) { continue; } // 可自定义过滤,改变路径 var filterResult = OnCollectDepPathFilter(depObj, depAssetPath, buildAssetPath); if (!filterResult) { continue; } var unityAssetType = GetUnityAssetType(depAssetPath); depObjectsMap[buildAssetPath] = new CollectedDepAssetInfo() { Asset = depObj, ExtType = depExtType, UnityAssetPath = depAssetPath, BuildAssetPath = buildAssetPath, UnityAssetType = unityAssetType, }; } var depObjectsList = depObjectsMap.Values.KToList(); var comparer = new DepListComaparer(); depObjectsList.Sort(comparer); return(depObjectsList); }