Loading and unloading a 360° Video

Tutorial

·

intermediate

·

+10XP

·

20 mins

·

(33)

Unity Technologies

Loading and unloading a 360° Video

When you use a 360° video in a Unity project, it's important to manage the memory the video uses. Unity's Addressables system provides a solution for managing memory for 360° videos that's easier than using the AssetBundles API.

Languages available:

1. Overview

When you use a 360° video in a Unity project, it's important to manage the memory the video uses. Unity's Addressables system provides a solution for managing memory for 360° videos that's easier than using the AssetBundles API.


With the Addressables system, you identify assets as addressable and load them into memory using an address. The Addressables API helps you manage not only content at the address, but also all dependencies of that content. The system notifies you when the entire asset is ready, so your video is loaded before the content is returned.


In this tutorial, you’ll use the Addressables asset system to mark a 360° Video Clip as addressable, create a video player, and then simply load clips into the Scene when they’re needed.


2. Prepare your project

This tutorial assumes that you already have an existing 360° video in the Unity project that you're working with. Or, you can create a new project using the VR template to get started, providing that you have access to at least one 360° video.


To set up a new base project for your 360° video, follow these instructions:


1. Open the Unity Hub and select New Project.


2. Set the Editor version 2022.2 and select the VR (Core) template.


3. Select Create Project.


4. Once the project is open, import your 360° video assets.


3. Install the Addressables package and configure your assets

Before setting up the video player, you’ll first configure your 360° videos to be addressable.



1. Open the Package Manager by going to Window > Package Manager, and in the Unity Registry locate the Addressables package. Download and install the package into your project.


2. Enable your video clips to be addressable. To do this, open the Addressables Groups window by navigating to Window > Asset Management > Addressables > Groups.


3. In the Addressables Groups window, select Create Addessables Settings.



4. Drag the video clip into the Addressables Groups window under the Default Local Group object to make it an addressable asset. Alternatively, you may select the Addressable option in the video clip’s Inspector.




5. In the Addressables Groups window, select Use Existing Build from the Play Mode Script options.



6. From the Build dropdown in Addressables Groups, select New Build > Default Build Script. If you see a prompt to enable a debug log layout, select No.


Note: You’ll need to repeat this step each time you add or remove addressable assets prior to running or building your project. This process simply lets the Addressables system know which assets to include in the project.



4. Create a Video Player to play the 360° Videos

The 360° video is now configured for use with the Addressables system. Next, you need to create a video player for testing purposes.


1. To add a video player to the Scene, right-click in the Hierarchy window and select Video > Video Player.


2. In the Project window, create a new render texture by right-clicking and selecting Create > Render Texture. Name it “VideoTexture”.


3. Create a new material, set its shader to Panoramic (Skybox > Panoramic), and name it “VideoMaterial”.


4. Assign VideoTexture to the Spherical (HDR) variable of VideoMaterial.


5. Open your Lighting Settings by going to Window > Rendering > Lighting. In the Environment tab, set the Skybox Material variable to VideoMaterial.


6. In the Video Player’s Inspector, set the Render Mode to Render Texture and add VideoTexture to the Target Texture reference.


The actual loading of the 360° video will be managed by a script that you will create in the next step. However, you may temporarily add the 360° video to the video player in order to ensure that it’s working as expected.


To test the video player, follow these instructions:


7. Select the Video Player GameObject, and in the Inspector, add the 360° video to the Video Clip parameter.


8. Playtest the scene. The video should begin to play as soon as the scene has loaded.


9. Remove the 360° video from the Video Clip parameter.


5. Create a button and script to load the videos at runtime

The video player is now functional, however without an assigned clip, it wont play anything. As a next step, you’ll add a script that will play the video on the player once it has been fully loaded via the Addressables system.


Follow these instructions to create a UI button to load the video:


1. In the Hierarchy, right-click and select UI > Button - Text Mesh Pro and name it "VideoButton". Change its child text to say "Load Video".


Configure the button in any other way you wish and place it somewhere accessible in your scene. With the button completed, you can now create the script.


2. In the Project window, create a new C# script and name it "VideoManager".


3. Copy the code below into your script and save it.


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

4. Create an empty GameObject in the scene and name it "VideoManager".


5. Add the VideoManager script to the VideoManager GameObject.


6. Assign the Video Player you created previously to the Video Player parameter, and add your selected 360° video to the Video Clip parameter.



7. The script is now ready to be called from the button. Select VideoButton, and press + to add a new OnClick() event.


8. Assign VideoManager to the object variable for the new event.


9. Click NoFunction and select VideoManager > LoadAndPlayVideo.


10. Playtest the game and press the button. The video should begin playing after it has finished loading.


Additional Notes


The Addressables system will automatically unload files each time a new scene is loaded. However, if you would like to unload the videos prior to a scene change, you can call the ReleaseAsset() method on the currently loaded asset.


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Using Addressables for Android devices requires you to use an uncompressed cache (current limitation for Unity video clips on Android). You can make the following calls in the Awake() method as a workaround:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

6. Conclusion

Nice work! You’ve implemented 360° video loading using Unity’s Addressables system. Now that you have a single video loading using the system, try creating a system to handle multiple videos.


Complete this tutorial