This is the 24th day of my participation in the August Text Challenge.More challenges in August

Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

Hello everyone, I am a Buddhist engineer ☆ quiet small magic dragon ☆, update Unity development skills from time to time, think useful remember one key three link oh.

One, foreword

In recent days, I have been studying the parsing and loading of The AssetBundle resource packaging box, and I have stepped on a lot of stumbles. I have referred to a lot of people’s articles, and FOUND that many people’s articles about the AssetBundle are either out of date, not working, a little bit messy, a little bit messy, or confused. People can not quickly get how to use this thing, so I deliberately after stepping on the pit to share the experience I learned to everyone.

PS: a bitter tears

Second, refer to the article

Unity5 automatically names assetBundles and packages assetBundles after 4.5.0 using 5.Unity AssetBundle Pit Crawl 6. More detailed introduce Unity5 AssetBundle 7. Institute of Unity3D AssetBundle ‘(sixty-one) 8. New AssetBundle Unity5 packaging BuildPipeline. BuildAssetBundles 9. Bundle and load the BundleAsset resource in Unity

PS: From the reference article to know how many pits


AssetBundle packaging

3.1 API

  1. BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
  2. BuildAssetBundles(string outputPath,AssetBundleBuild[] builds,BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

3.2 Parameter Description

OutputPath: the directory where the Bundle is stored. For example: Assets/AssetBundles, this folder does not automatically created, if it doesn’t exist, the function will fail BuildAssetBundleOptions: Bundle packaging, don’t have any special options none. BuildTarget: Build platforms, such as iPhone, Windows, Android, etc

AssetBundleBuild[]: This class is used with BuildAssetBundles. Specify the name assetBundleName for a Bundle and the names of the resources (such as multiple images, audio, and so on) it will contain. The array of AssetBundleBuild[] elements passed to the function is called a “build map” and serves as an alternative to specifying the contents of the editor package. AssetBundleBuild[] variable: addressableNames: Returns all addressableName array assetBundleName: assetBundleVariant: Unity AssetBundle: Specifies the names of all resources that belong to an addressableName name. This is an array of all resource names that belong to an addressableName name

3.3 example

  1. Create a new script, PackBundles. Cs, and place it in the Editor folder

2. Edit codeThe source code:

using System.Collections.Generic;
using System.IO;
using UnityEditor;

public class PackBundles : Editor
{
    // Select resource package
    [MenuItem("PackBundles/PackBundles")] 
    static void PutBundleAssetes()
    {
    	// Initialize an AssetBundleBuild table
        List<AssetBundleBuild> buildMap = new List<AssetBundleBuild>();
        AssetBundleBuild build = new AssetBundleBuild();
        // Set the AssetBundleBuild name and the resource path
        build.assetBundleName = "123.unity3d";
        build.assetNames = new[] { "Assets/Textures/123.jpg" };
        // add to table
        buildMap.Add(build);

        // Put these resource bundles in a directory called ABs
        string assetBundleDirectory = "Assets/ABs";
        // If the directory does not exist, create one
        if(! Directory.Exists(assetBundleDirectory)) { Directory.CreateDirectory(assetBundleDirectory); }// Resource packaging
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, buildMap.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }

    // Wrap it all up
    [MenuItem("PackBundles/AllPackBundles")] 
    static void PutBundleAssetesAll()
    {
        // Put these resource bundles in a directory called ABs
        string assetBundleDirectory = "Assets/ABs";
        // If the directory does not exist, create one
        if (!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        BuildPipeline.BuildAssetBundles(assetBundleDirectory,BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

Copy the code

3. Run

First put an image of 123.jpg in the Textures folder Click PackBundlesThis packs 123.jpg from Texture into 123.unity3D

Second, all allPackage bundles need to be set in the editor first And then packagedThis packs a prefab named Cube into Cube. Unity3d

AssetBundle parsing load

4.1 API on

4.1.1 Loading the API of AssetBundle

Public static AssetBundle LoadFromFile(string path); public static AssetBundle LoadFromFile(string path ,uint crc = 0); public static AssetBundle LoadFromFile(string path ,uint crc = 0, ulong offset= 0);

4.1.2 Parameter Description

CRC: Offset: This value specifies where the AssetBundle is read from

Public static UnityWebRequest GetAssetBundle(string URI, uint CRC); public static UnityWebRequest GetAssetBundle(string uri, uint version, uint crc); public static UnityWebRequest GetAssetBundle(string uri, Hash128 hash, uint crc); public static UnityWebRequest GetAssetBundle(string uri, CachedAssetBundle cachedAssetBundle, uint crc);

4.1.3 Parameter Description

Uri: network address of the AssetsBundle: (can be local file:) CRC: 0 If it is not 0, verification will be performed. Version: an integer version number Hash: a version hash cachedAssetBundle: Structure used to download a given version of the AssetBundle to a custom cache path

4.2 Loading the Resource API in the Package

Public Object LoadAsset(string name); public Object LoadAsset( string name, Type type); public T LoadAsset( string name); public Object[] LoadAllAssets( Type type); public Object[] LoadAllAssets(); public T[] LoadAllAssets(); public Object LoadAllAssetsAsync(); public Object LoadAssetAsync( string name);

4.2.1 Parameter Description

Name: loads a resource named name from the AssetBundle and returns object Type: loads all resources of Type in the package LoadAllAssets: loads all resources in the package LoadAssetAsync: loads all resources in the package asynchronously

4.3 other apis

Path.Combine(string, String) to connect two string Application. StreamingAssetsPath output E: / UnityProject/ARVR/Workspace/MyCharacter/Assets/StreamingAssets Application. DataPath output E: / UnityProject/ARVR/Workspace/MyCharacter/Assets

4.2 Downloading Resources from the Internet

The source code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadBundles : MonoBehaviour
{
    GameObject go;
    void Start()
    {
        StartCoroutine(Load());
    }

    IEnumerator Load()
    {
        // Download and load from a remote server
        WWW www = new WWW("ftp://123.778.1.128/cube.unity3d");
        // Wait for the file to download
        yield return www;
        // Load the data and assign it to AssetBundle
        AssetBundle bundle = www.assetBundle;
        // The LoadAssetAsync asynchronous load package is passed to request, which holds data named Cube. Prefab
        AssetBundleRequest request = bundle.LoadAssetAsync("Cube.prefab".typeof(GameObject));
        go = Instantiate(request.asset as GameObject, new Vector3(0f.0f.0f), Quaternion.identity) as GameObject;
        yield return request;
        // Release resourceswww.Dispose(); }}Copy the code

4.3 Downloading Resources from the Local PC

The source code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadBundles : MonoBehaviour
{
    GameObject go;
    void Start()
    {
        StartCoroutine(Load());
    }

    IEnumerator Load()
    {
        // Load from a local file
        WWW www = new WWW("file://D:/Frank/UnityProject/Demo-1/Assets/ABs/cube01.unity3d");
        // Wait for the file to download
        yield return www;
         // Load the data and assign it to AssetBundle
        AssetBundle bundle = www.assetBundle;
        // The LoadAssetAsync asynchronous load package is passed to request, which holds data named Cube. Prefab
        AssetBundleRequest request = bundle.LoadAssetAsync("Cube.prefab".typeof(GameObject));
        go = Instantiate(request.asset as GameObject, new Vector3(0f.0f.0f), Quaternion.identity) as GameObject;
        yield returnrequest; www.Dispose(); }}Copy the code

Five, test examples

# # steps:

  1. Create a new script, PackBundles. Cs, and place it in the Editor file

Edit the script2. Create a new script, LoadBundles. Cs, and place it wherever you like:3. Package resources 4. Mount the loading script LoadBundles. Cs to any object in the scene5. The load is successfully generated

PS: The Cube on the way is the prefabricated Cube set by me, with a material added

OK, end of article

PS: If you want to load online resources, just put the package on the server, and then download it through the address, WWW class, parse and load it.