Aller au contenu

Working with Assets Bundles

URP + VR

When using an Asset Bundle with the URP renderer in VR, special provisions must be integrated. A section at the bottom of the page indicates how to produce them.

Creation of Assets bundles

Assets Bundles compilation script

The creation of Unity Assets Bundles is simple and requires only a few actions. However, a script must be produced to generate the files that will be used later. This script is always the same and can be found in the official Unity documentation:


using UnityEditor;
using System.IO;

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string assetBundleDirectory = "Assets/AssetBundles";
        if (!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
}

!

The asset placed in the project folder.

The .Cs script should be placed in the Editor folder of the Assets folder of the Unity project. After this editor script is integrated, the menu bar is expanded.

!

The asset placed in the project folder.

Asset Bundle referencing and generation

!

A Unity scene with an Asset Bundle name (The scene has no direct lighting).

The built-in script is not enough to create the Asset Bundles. Indeed, we now need to declare the objects and scenes that will be embedded in the Assets Bundles files. To do this, we need to go into the Unity project area and select the items one by one so as to assign them a container name:

!

On the bottom right, the referencing area of the Asset Bundle containing our scene (here: boatsceneexport2.boat).

When generating via the Asset Bundles creation script, a special folder named AssetBundles is created in the project directory. This folder contains all the created Assets Bundles files.

!

The created Assets Bundles files.

In our case, only the boatsceneexport2.boat file is important, the other files are useless however, it is important to note that it is possible that one Asset Bundle references another Asset Bundle file, so for simplicity, it is appropriate to copy the entire contents of the generated folder into the VRmaze user data folder.

Copy to VRmaze

It is necessary to copy the data to the VRmaze user data folder, because the software will only load data if it is present in this folder.

Using asset bundles

When the asset bundle is copied to the VRmaze data folder, it becomes usable. VRmaze provides two types of asset bundle loaders. The first one allows the loading of prefabs and offers the possibility to assign them a position and an orientation as well as a specific behavior. The second one consists in loading a complete scene on which there will be no direct control. This second option is interesting, because it allows you to exploit light textures for example.