unity加载ab包

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;

public class Test : MonoBehaviour {

	// Use this for initialization
	IEnumerator/void Start () {
	//1.
     //没有分包加载
     //AssetBundle ab =AssetBundle.LoadFromFile("AssetBundles/model/cube.unity3d");//加载ab
     //GameObject go=  ab.LoadAsset("cube");
     //Instantiate(go);

     //分了依赖后的加载方式
     //AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/model/cube.unity3d");//加载ab    b包
     //AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/share");//a包
     //GameObject go = ab.LoadAsset("cube");
     //Instantiate(go);  

        /*分包注意的问题:
         * 1.依赖包重复的问题
         * 2.把需要共享的资源打包到一起
         * 3.图集重复问题
         * 
         * ab分组策略:
         * a.逻辑实体分组:
         * 1.一个UI界面或者所有的UI界面一个包(这个界面里面的贴图和布局信息一个包)
         * 2.一个角色或者所有角色一个包(这个角色里面的模型和动画一个包)
         * 3.所有场景所共享的部分一个包(包括贴图和模型)
         * b.按照类型分组
         * 1.所有的音频资源打成一个包
         * 2.所有的shader打成一个包
         * 3.所有模型打成一个包
         * 4.所有材质打成一个包
         * c.按照使用分组
         * 1.按照关卡(角色,贴图,音频)
         * 2.按照场景(一个场景所需要的资源打成一个包)
         * 
         * 1.经常更新的资源放在一个单独的包里面,跟不经常更新的包分离
         * 2.把需要同时加载的资源放在一个包里面
         * 3.可以把其他包共享的资源放在一个单独的包里面
         * 
         */

		//2.
        //从内存中异步加载LoadFromMemoryAsync
        string path = "AssetBundles/cube.unity3d";
        //AssetBundleCreateRequest request=AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));//异步加载
        //yield return request;//等待加载完成
        //AssetBundle ab = request.assetBundle;
        //AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path)); //同步加载
        //GameObject go = ab.LoadAsset("Cube");
        //Instantiate(go);

        //从本地文件中加载
        //AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);//异步的方式
        //yield return request;
        //AssetBundle ab = request.assetBundle;
        //GameObject go = ab.LoadAsset("Cube");
        //Instantiate(go);

        //第三种 www 本地加载和服务器加载
        //while(Caching.ready==false)
        //{
        //    yield return null;
        //}
        ////WWW www = WWW.LoadFromCacheOrDownload(@"file://E:assetBundle\AssetBundle\AssetBundles\cube.unity3d", 1);//从本地加载资源文件
        //WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cube.unity3d", 1);//从服务器进行资源加载
        //yield return www;
        //if (string.IsNullOrEmpty(www.error)==false)//判断下载是否有错,有错还是会下载,但是需要输出错误信息
        //{
        //    Debug.Log(www.error);
        //    yield break;
        //}
        //AssetBundle ab = www.assetBundle;
        //GameObject go = ab.LoadAsset("Cube");
        //Instantiate(go);

        //第四种加载方式 unitywebrequest 本地加载和服务器加载
        //string url = @"file://E:assetBundle\AssetBundle\AssetBundles\cube.unity3d";
        string url = @"http://localhost/AssetBundles/cubewall.unity3d";
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);//发起请求
        yield return request.SendWebRequest();//从服务端开始时下载,等待下载完成
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);//获取ab包里面的资源
        GameObject go = ab.LoadAsset<GameObject>("CubeWall");

       AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");//从本地加载assetbundles里的manifset
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//从manifset里去加载资源
        foreach (string name in manifest.GetAllAssetBundles())//获取manifset里assetbundle包的所有名字
        {
            print(name);
        }
        string[] strs = manifest.GetAllDependencies("cubewall.unity3d");//获取manifset里cubewall.unity3d包里所依赖的包
        foreach (string name in strs)
        {
            print("name:"+name);
            AssetBundle.LoadFromFile("AssetBundles/" + name);//把依赖的包进行加载出来,这样就可以把cubewall.unity3d所依赖的包都加载出来了
        }
        Instantiate(go);
    }
}

你可能感兴趣的:(Unity)