Use case: Load a Model From StreamingAssets
This use case outlines the basics of setting up a streaming-ready scene. Use this use case to learn how to stream a model from the project StreamingAssets directory.
To accomplish this use case, do the following:
- Save a model in the
StreamingAssets
directory. - Create an offline streaming behavior.
- Set up a new scene.
How do I...?
Save a model in the StreamingAssets directory
Copy a data-streaming model1 into the StreamingAssets
directory and name your copied directory MyModel
. Make sure the tileset.json
file is inside the MyModel
directory.
Create an offline streaming behavior
To create a offline streaming behavior, follow these steps:
- Open your Unity project.
- In the Project window, select and hold the Assets folder .
- Select Create > C# Script.
- Rename the new script as
OfflineDataStreaming
. - Open the
OfflineDataStreaming
script and replace the content with the following:using System; using System.IO; using Unity.Cloud.DataStreaming.Runtime; using UnityEngine; public class OfflineDataStreaming : MonoBehaviour { [SerializeField] Camera m_Camera; [SerializeField] string m_Path; [SerializeField] float m_ScreenSpaceError = StageObserverFactory.DefaultScreenSpaceError; IDataStreamer m_Streamer; void Start() { // Create the settings for our streamer. var builder = DataStreamerSettingsBuilder.CreateDefaultBuilder(); var uri = new Uri( new Uri($"{Application.streamingAssetsPath}{Path.DirectorySeparatorChar}"), m_Path); var settings = builder.Build(); // Start to stream. m_Streamer = IDataStreamer.Create(); var observer = StageObserverFactory.CreateCameraObserver( m_Camera, m_ScreenSpaceError); var stage = m_Streamer.Open(settings); stage.Observers.Add(observer); stage.Models.Add(x => x.FromUri(uri)); } void OnDestroy() { m_Streamer?.Close(); } }
Set up a new scene
To set up a new scene, follow these steps:
Create a new scene.
Create a GameObject called Streamer.
In the Inspector window, select Add Component.
Add the Offline Data Streaming component.
In the Path inspector field, write
MyModel/tileset.json
.Add a Camera in your scene if one doesn't exist.
Set the OfflineDataStreaming component Camera value to your scene camera.
Select Play to view your model load in front of the camera.2
1To generate a model from the Asset Manager, refer to the Use case: Generate an offline dataset
2 If the model does not load in front of the camera, select the menu item Edit / Frame Streamed Model
to frame the model in the Unity Scene View.