Unity学习(六)5.x依赖打包

http://blog.sina.com.cn/s/blog_89d90b7c0102w2ox.html
unity5已经封装好了接口,所以依赖打包并没有那么神秘和复杂了。


打包:
1.定义好资源的assetBundleName
2.BuildPipeline.BuildAssetBundles,指定资源目录和压缩类型


生成:
1.Assetbundle文件,加载时的首要文件,包含所有资源的依赖信息
2.每个文件对应一个.manifest,不用管他,但是可以打开查看他引用了哪些资源。


加载:
1.获取AssetBundle文件
2.LoadAsset("AssetBundleManifest")转换为AssetBundleManifest
3.通过manifest.GetAllDependencies("测试文件"),获取它依赖的ab,得到的是AB数组,并下载它


4.最后下载名为(测试文件)的资源即可。


测试代码:
using UnityEngine;  
using System.Collections;  
  
public class LoadAssetbundle : MonoBehaviour {  
      
    void Start()  
    {  
        // 1.加载Manifest文件  
        AssetBundle manifestBundle=AssetBundle.CreateFromFile(Application.dataPath +"/ab/Assetbundle");  
        if(manifestBundle != null)  
        {  
            AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");  
            // 2.获取依赖文件列表  
            string[] cubedepends = manifest.GetAllDependencies("assets/res/1.prefab");  
            AssetBundle[] dependsAssetbundle = new AssetBundle[cubedepends.Length];  
            for(int index = 0; index < cubedepends.Length; index++)  
            {  
                // 3.加载所有的依赖资源  
                dependsAssetbundle[index]=AssetBundle.CreateFromFile(  
                    Application.dataPath +"/../Assetbundle/"+cubedepends[index]);  
            }  
  
            // 4.加载资源  
            AssetBundle cubeBundle=AssetBundle.CreateFromFile(  
                Application.dataPath +"/ab/assets/res/1.prefab" );  
            GameObject cube=cubeBundle.LoadAsset("1") as GameObject;  
            if(cube!=null)  
            {  
                Instantiate(cube);  
            }  
        }  
    }  



坑tips:
如果材质球的名称和它引用的贴图名称一样,材质球内存占有量就会像包含了贴图的内存一样。


换版本tips:
4.6项目移植到5.0.2,ab包无法加载,重新打包即可。
不改打包代码,多数文件大小增大2k
改为最新打包代码,不做依赖,多数文件大小增大2-4%左右


tips:
如果做依赖,会生成很多零碎的文件,开发期不建议使用,以免增加不必要的工作量。和美术定义好资源规范才是重点。(个人意见,不喜随你便Unity学习(六)5.x依赖打包)
原文地址:https://www.cnblogs.com/nafio/p/9137399.html