Unity默认有2个内部资源文件分别是:
Library/unity default resources
Resources/unity_builtin_extra
在打包AssetBundle时,如果AB中有引用到这些的话,会直接把需要的内容打包到AB中,但是这样如果细分打包的话,每个小的包就会重复打包这些内部资源,最后整个包体就会变大.
今天想了想能不能直接将这2个资源直接添加到打包的AB中,从而能想其他外部资源一样让Unity自动依赖这些文件,减小包体呢?
于是做了如下测试.
private void BuildBultinRes()
{
string outputpath = "Assets/BuiltinRes/";
if (Directory.Exists(outputpath))
{
Directory.Delete(outputpath, true);
}
Directory.CreateDirectory(outputpath);
List<AssetBundleBuild> buildlist = new List<AssetBundleBuild>();
//Library/unity default resources
AssetBundleBuild lib = new AssetBundleBuild();
lib.assetBundleName = "lib.unity3d";
lib.assetNames = new string[] { "Library/unity default resources" };
buildlist.Add(lib);
//Resources/unity_builtin_extra
AssetBundleBuild exres = new AssetBundleBuild();
exres.assetBundleName = "exres.unity3d";
exres.assetNames = new string[] { "Resources/unity_builtin_extra" };
buildlist.Add(exres);
//ui/launch.unity3d
//Assets/GameData/UI/Prefab/Launch/Panel_launch.prefab
AssetBundleBuild launch = new AssetBundleBuild();
launch.assetBundleName = "launch.unity3d";
launch.assetNames = new string[] { "Assets/GameData/UI/Prefab/Launch/Panel_launch.prefab" };
buildlist.Add(launch);
BuildAssetBundleOptions options = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.AppendHashToAssetBundleName;
AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(outputpath, buildlist.ToArray(), options, EditorUserBuildSettings.activeBuildTarget);
}
结果打包可以正常打,但是并不会打出期望的lib.unity3d和exres.unity3d,而其他如launch.unity3d等正常外部文件可以打出,也就是说加入Library/unity default resources和Resources/unity_builtin_extra和正常不加结果是完全一样的.
所以不能通过这种方式减小包体积.
那么应该怎么去掉这些内部资源呢?不知道未来Unity官方会不会注意到这问题,提供一种官方的解决方式.