Precautions for packaging and loading with AssetBundle in Unity

1. Use AssetBundle for packaging and loading
Personally, I think you can refer to the following two blogs. There are many related tutorials on the Internet. If you don’t understand, you can go to Baidu or leave a message.
Unity packaging and loading AssetBundle (loading the corresponding dependencies)
AssetBundle in Unity
2. Precautions when using AssetBundle to load:
Note 1: The name of the AssetBundle and the prefab that sets the AssetBundle name must be the same as those in the C# code method The parameters correspond one-to-one.
If it does not correspond, this error will be reported in the Unity console: ArgumentException: The Object you want to instantiate is null.
insert image description here

1. This is the prefab I want to package.
insert image description here
It can be seen that the name of the AssetBundle corresponding to the prefab of TestCapsule is set to test_1
2. I use the AssetBundle Browser, an official AB package management plug-in provided by Unity, for packaging. Readers can use custom scripts for packaging. Personally, I just think that this way of plug-in packaging is very useful.
insert image description here
After clicking the Build button, a StreamingAssets folder will be generated under the project Assets folder. Inside the StreamingAssets folder is the typed AssetBundle file.
insert image description here
insert image description here

After clicking the Build button, there is an AssetBundles folder in the same level directory as the Assets folder of the project, and the AssetBundle file is also typed out in this AssetBundles folder.
insert image description here

insert image description here

Note: The contents of the AssetBundle file in the StreamingAssets folder and the AssetBundles folder are the same.

3. Use C# code to load the AssetBundle resource package (local relative path loading).

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

public class TestABLoad : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        AssetBundle ab = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "test_1"));
        GameObject cube = ab.LoadAsset("TestCapsule") as GameObject;//LoadAsset方法中参数为导出前Prefab名字
        Instantiate(cube);//生成
    }
}

It should be noted here that the parameter in the AssetBundle.LoadFromFile method is the path of the AssetBundle file that is typed out.
insert image description here
That is, in the StreamingAssets directory under the Assets directory, that is, the full name of the test_1 file (if there is a suffix, it must include the suffix, and if there is no suffix, it can not be included).
insert image description here
Also, the parameters in the LoadAsset method in the figure below must be consistent with the name of the prefab that sets the AssetBundle file name test_1.
insert image description here
insert image description here
4. After saving the script, go back to Unity and mount the TestABLoad script on any game object in the game. After running the game, you can see that the game resources corresponding to the resource package test_1 are loaded.
insert image description here
Note 2: Use C# code to load the local absolute path AssetBundle resource package
1. As mentioned above, there is an AssetBundles folder in the same level directory as the Assets folder of the project, and the AssetBundle file is also typed out in the AssetBundles folder. The contents of the AssetBundle file in the StreamingAssets folder and the AssetBundles folder are the same. So you can also use the AssetBundles file in the AssetBundles folder to load.
insert image description here
It can be seen that this folder is not in the project Assets directory, so use an absolute path to load it.

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

public class TestABLoad : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        var myLoadedAssetBundle = AssetBundle.LoadFromFile("G:\\C++ Open Sources Project\\NoahGameFrameClient\\UnityDemo\\AssetBundles\\StreamingAssets\\test_1");
        if (myLoadedAssetBundle == null)
        {
    
    
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("TestCapsule");
        Instantiate(prefab);
    }
}

2. After saving the script, go back to Unity and mount the TestABLoad script on any game object in the game. After running the game, you can see that the game resources corresponding to the resource package test_1 are loaded.
insert image description here
3. But I did a small test. I mounted this TestABLoad script on a game object in another Unity project scene in my local area. After the game was running, the corresponding resources were not loaded, and an error would be reported. So it can be seen that the AssetBundles file typed out by the X project can only be used by the X project, and other projects cannot be used.
insert image description here
insert image description here
Note 3: Comparison of three compression methods:
NoCompression: no compression, fast decompression, large package, not recommended.
LZMA: Minimal compression, slow decompression, use one resource to decompress all resources under the package.
LZ4: slightly larger compression, fast decompression, what to use for decompression, low memory usage, generally recommended to use this.

1. Take the prefab that I want to package as an example. It can be seen that the prefab file occupies 3KB .
insert image description here

insert image description here
2. When I choose NoCompression to not compress, the decompression is fast, and the package is large. This method is not recommended, and I package it. It can be seen that the packaged file occupies 138KB .
Please add a picture description

Please add a picture description
3. When I choose LZMA: minimum compression, slow decompression, and use a resource to decompress all resources under the package , and package it, it can be seen that the packaged file occupies 28KB .
Please add a picture description
Please add a picture description
4. When I choose LZ4: Larger compression, faster decompression, what to use for decompression, low memory usage, it is generally recommended to use this method for packaging. It can be seen that the packaged file occupies 45KB .
Please add a picture description
Please add a picture description
Note 4: Delete the TestCapsule prefab corresponding to the AssetBundle resource package test_1, and the TestCapsule prefab can be loaded normally after the project runs.
1. This is the TestCapsule prefab corresponding to the AssetBundle resource package test_1.
insert image description here
2. Delete the TestCapsule prefab from the current project.
insert image description here
3. Mount the above relative path or absolute path TestABLoad script to a certain game object in the scene. After running the game, you can see that the game resources corresponding to the resource package test_1 are loaded.

insert image description here
This is loaded using a local absolute path.

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

public class TestABLoad : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        var myLoadedAssetBundle = AssetBundle.LoadFromFile("G:\\C++ Open Sources Project\\NoahGameFrameClient\\UnityDemo\\AssetBundles\\StreamingAssets\\test_1");
        if (myLoadedAssetBundle == null)
        {
    
    
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("TestCapsule");
        Instantiate(prefab);
    }
}

Next, we use the local relative path to load. After running the game, you can see that the game resources corresponding to the test_1 resource package are loaded.
insert image description here

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

public class TestABLoad : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        AssetBundle ab = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "test_1"));
        GameObject cube = ab.LoadAsset("TestCapsule") as GameObject;//LoadAsset方法中参数为导出前Prefab名字
        Instantiate(cube);//生成
    }
}

It can be seen from the above that the loading of resources does not depend on the prefab TestCapsule corresponding to the AssetBundle resource package test_1, but depends on the corresponding AssetBundle resource package test_1.

Guess you like

Origin blog.csdn.net/jianjianshini/article/details/129467281