How to use AB packaging

Posted by Izzy1979 on Sun, 17 Nov 2019 22:03:57 +0100

Use of AB packaging

Preface

This article only has the use method after packing. Please check my last blog for the packing method
https://blog.csdn.net/weixin_42746271/article/details/89791901

Loading method

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";
    // AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/tt/wall.unity3d");
    // // GameObject wallPrefab=   ab.LoadAsset<GameObject>("wall");
    // //Instantiate(wallPrefab);
    //Object[] objs= ab.LoadAllAssets();
    // foreach (Object o in objs)
    // {
    //     Instantiate(o);
    // }
    //StartCoroutine(LoadFromMemoryAsync());
    // LoadFromMemory();
    //  StartCoroutine(LoadFromCache());
    StartCoroutine(LoadFromUnityWebRequest());
}
/// <summary>
///The first way to load AB is to load it asynchronously from memory
/// </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>
///The first way to load AB is to load AB synchronously from memory
/// </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>
///The second way to load AB is to load it asynchronously from the local
/// </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>
///The third way to load AB package is from cache or server (deprecated)
/// </summary>
IEnumerator LoadFromCache()
{
    while (!Caching.ready)
    {
        //Pause a frame, continue to loop, judge when caching is ready
        yield return null;
    }
    //From local
    //WWW www= WWW.LoadFromCacheOrDownload(@"file://D:\UnityStudy\MyAB\AssetBundles\tt\wall.unity3d", 1);
    //from server
    WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/wall.unity3d", 1);
    //Waiting for download to complete
    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>
///The fourth method, the most important one (replacing the WWW class)
/// </summary>
/// <returns></returns>
IEnumerator LoadFromUnityWebRequest()
{//From local
  string uri = @"file:///Project of D:\Unity2017 \ abpackaging test \ AssetBundles\kaka.luete ";
 //from server
   // string uri = @"http://localhost/AssetBundles/wall.unity3d";
    UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
    //Download using the Send method
    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);
    }
}

}

explain

There are four ways to load. This time, use the local loading method (the last one)

First remember to change the path to your local path

Then change your name, which is the name of your AB package

Finished