Unity 2017版本的Assetbundle(解包)

上章说到了AssetBundle打包,这章就说下加载包,加载包官方上基本没什么变化,跟以前的差不多,只是多了个AssetBundle.LoadFromStream说是减少了转换格式的流程,其实并没有减少什么,所以2017的解包还是一样,先说说集中解包方式吧,这里就写简单普遍的2种吧,很多时候看多了也是各种晕,其他几种也是这2种延伸过去的

  1:从本地加载,解包总共有3中类型加载,6个方法,

    (1)AssetBundle.LoadFromFile:从本地文件同步加载;

          AssetBundle.LoadFromFileAsync:从本地文件异步加载;

    (2)AssetBundle.LoadFromMemory:从内存中加载,参数是个字节数组;

         AssetBundle.LoadFromMemoryAsync:从内存中异步加载;

     (3)AssetBundle.LoadFromStream:从IO流里同步加载,参数是个IO流;

         AssetBundle.LoadFromStreamAsync:从IO流里异步加载;

   我们先从本地加载吧,一般AseetBundle包的放在StreamingAssets下,这样在安卓平台下也方便加载

   void LoadAssetBundle (name){

     string path2 = Application.dataPath + "/StreamingAssets/"+name;  

//根据本地文件地址进行加载AssetBundle

     AssetBundle asset2 = AssetBundle.LoadFromFile(path2);

    if(asset2!=null){

//AssetBundle 加载资源,方法也有同步异步,大家也可以去看下官方文档,跟加载AssetBundle一样

     Gameobject obj=asset2.LoadAsset(name) as GameObject;

//实例化资源

     Instantiate(obj);

  }

}

2.从服务器加载,使用www类和协程下载

    IEnumerator Load(string url) {
        WWW www =new WWW(url);
        yield return www;
        AssetBundle asset = www.assetBundle;
        using (MemoryStream steam=new MemoryStream() ) {
            BinaryWriter writer = new BinaryWriter(steam);
            writer.Write(asset);
            AssetBundle asset1 = AssetBundle.LoadFromStream(steam);
            Object obj = asset1.LoadAsset(name);//加载的资源名字
            Instantiate(obj);
        }


        }

这种我强行用新版本出来的加载流的的方法写的,根据自己理解也没经过测试,只是让大家便于理解

好了,整体来说就这些了,新版本的Assetbundle大致就这些了,欢迎小伙伴一起讨论研究,如果有什么意见或者不同的看法想法的留言,我看到了必定一一回应,还有我写的都是原创,未经过本人允许请勿转载。

  

你可能感兴趣的:(Unity)