天天看点

AB打包的使用方法前言加载方法讲解

AB打包的使用

  • 前言
  • 加载方法
  • 讲解

前言

本文章只有打包完的使用方法 ,需要打包的方法查看我的上一篇博客

https://blog.csdn.net/weixin_42746271/article/details/89791901

加载方法

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

public class LoadFromFileExample : MonoBehaviour {

    private string path;
    void Start()
    {
        path = "AssetBundles/kaka.luete";
        //从内存中异步加载
        StartCoroutine(LoadFromMemoryAsync());
        //内存中同步加载
        LoadFromMemory();
        //本地中异步加载
        StartCoroutine(LoadFromFileAsync());
        //从缓存或服务器上加载
		StartCoroutine(LoadFromCache());
		//第四种方法,最常用的方法,使用UnityWebRequest加载
		StartCoroutine(LoadFromUnityWebRequest());

    }
    /// <summary>
    /// 第一种加载AB的方式,从内存中异步加载
    /// </summary>
	IEnumerator LoadFromMemoryAsync()
    {
        AssetBundleCreateRequest requst = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return requst;
        AssetBundle ab = requst.assetBundle;
        GameObject wallPrefab = ab.LoadAsset<GameObject>("wall");
        Object[] objs = ab.LoadAllAssets();
        foreach (Object o in objs)
        {
            Instantiate(o);
        }
    }
    /// <summary>
    /// 第一种加载AB的方式,从内存中同步加载
    /// </summary>
    void LoadFromMemory()
    {
        //AssetBundleCreateRequest requst = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));

        //AssetBundle ab = requst.assetBundle;
        AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
        GameObject wallPrefab = ab.LoadAsset<GameObject>("wall");
        Object[] objs = ab.LoadAllAssets();
        foreach (Object o in objs)
        {
            Instantiate(o);
        }
    }
    /// <summary>
    /// 第2种加载AB的方式,从本地中异步加载
    /// </summary>
    IEnumerator LoadFromFileAsync()
    {
        AssetBundleCreateRequest requst = AssetBundle.LoadFromFileAsync(path);
        yield return requst;
        AssetBundle ab = requst.assetBundle;
        GameObject wallPrefab = ab.LoadAsset<GameObject>("wall");
        Object[] objs = ab.LoadAllAssets();
        foreach (Object o in objs)
        {
            Instantiate(o);
        }
    }
    /// <summary>
    /// 第三种加载AB包方式,从缓存或服务器上加载(已经弃用)
    /// </summary>
    IEnumerator LoadFromCache()
    {
        while (!Caching.ready)
        {
            //暂停一帧继续循环判断caching什么时候ready
            yield return null;
        }
        //从本地
        //WWW www= WWW.LoadFromCacheOrDownload(@"file://D:\UnityStudy\MyAB\AssetBundles\tt\wall.unity3d", 1);
        //从服务器
        WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/wall.unity3d", 1);
        //等待下载完成
        yield return www;
        if (string.IsNullOrEmpty(www.error) == false)
        {
            Debug.Log(www.error);
            yield break;
        }
        AssetBundle ab = www.assetBundle;
        GameObject wallPrefab = ab.LoadAsset<GameObject>("wall");
        Object[] objs = ab.LoadAllAssets();
        foreach (Object o in objs)
        {
            Instantiate(o);
        }
    }
    /// <summary>
    /// 第四种方法,最重要的一种方法,使用比较多(取代WWW类)
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadFromUnityWebRequest()
    {//从本地
      string uri = @"file:///D:\Unity2017的工程\AB打包试验\AssetBundles\kaka.luete";
     //从服务器
       // string uri = @"http://localhost/AssetBundles/wall.unity3d";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        //使用Send方法进行下载
        yield return request.Send();
        //  AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        GameObject wallPrefab = ab.LoadAsset<GameObject>("kaka");
        Object[] objs = ab.LoadAllAssets();
        foreach (Object o in objs)
        {
            Instantiate(o);
        }
    }

}
           

讲解

上边呢是使用了四种方法加载,本次使用本地加载方法(最后一种)

首先记得修改路径为你的本地路径

AB打包的使用方法前言加载方法讲解

然后呢修改你的名字 ,就是你的AB包的名字

AB打包的使用方法前言加载方法讲解

完事