加载AB包基础

加载AB包注意事项

1.基础的正确的加载方式
在这里插入图片描述
2.有问题的基础的加载方式
在这里插入图片描述
3.总结 :在加载AB包资源的时候 必须先加载资源所依赖的资源 不然可能会造成贴图或者其他成分丢失 这个值得注意

Manifest基础解释

  • Dependencies :当前资源所依赖的资源
  • Assets:当前资源的路径

在这里插入图片描述

从内存中加载AB包

  //2.从内存加载AB包

        string path2 = "C:/Users/Administrator/Desktop/WorkSpace5.6/AB_Packet/Assets/AssetBundles/cubewall";
        //异步返回的是一个请求Request
        AssetBundleCreateRequest request1 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
        AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
        yield return request2;

        //同步方式直接返回的是AssetBundle
        AssetBundle.LoadFromMemory(File.ReadAllBytes(path2));
        AssetBundle ab = request2.assetBundle;
        GameObject cube = (ab.LoadAsset("Cube")) as GameObject;
        Instantiate(cube);

从服务器加载AB包


        //3.从服务器种加载AB包的方式www
     IEnumerator Start()
    {
        //string path2 = "C:/Users/Administrator/Desktop/WorkSpace5.6/AB_Packet/Assets/AssetBundles/cubewall";
        string path2 = @"http://localhost/AssetBundles/cubewall";
        WWW www = WWW.LoadFromCacheOrDownload(path2, 1);
        while (Caching.ready == false)
        {
            yield return null;
        }
        yield return www;
        if (string.IsNullOrEmpty(www.error) == false)
        {
            Debug.Log(www.error);
            yield break;
        }
        AssetBundle ab = www.assetBundle;
        //使用
        GameObject Cube = ab.LoadAsset<GameObject>("Cube");
        Instantiate(Cube);
    }
   

通过WebRequest加载

    //第4种方式通过WebRequest

    IEnumerator Start()
    {


        string uri = @"http://localhost/AssetBundles/cubewall";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        yield return request.Send();
        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        DownloadHandlerAssetBundle ab = request.downloadHandler as DownloadHandlerAssetBundle;
        GameObject Cube = ab.assetBundle.LoadAsset<GameObject>("Cube");
        Instantiate(Cube);

        //通过加载Mainifest文件来加载资源所依赖的包


        AssetBundle assetBundle = AssetBundle.LoadFromFile(@"C:\Users\Administrator\Desktop\WorkSpace5.6\AB_Packet\Assets\AssetBundles\AssetBundles");
        AssetBundleManifest abManifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        foreach (string name in abManifest.GetAllDependencies("cubewall"))
        {
            print(name);
            AssetBundle.LoadFromFile(@"C:\Users\Administrator\Desktop\WorkSpace5.6\AB_Packet\Assets\AssetBundles\" + name);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值