Use case: Link the Camera to the DataStreamer
This use case outlines the basics of setting up a camera in a streaming scene. Use this use case to learn how to set the camera observer on a DataStreamer and dynamically set its Far Clipping Plane.
Prerequisites
To accomplish this use case, you require the following:
- Install the Data Streaming package on a new or existing Unity project (refer to Installation).
- Add these packages to the
manifest.json:
{
"dependencies": {
// Add these lines:
// Replace "<x.y.z>" with the version you wish to install
"com.unity.cloud.identity": "<x.y.z>",
"com.unity.cloud.storage": "<x.y.z>"
// Other dependencies...
}
}
Overview
To accomplish this use case, do the following:
- Upload a scene
- Create a cloud streaming behavior
- Set up a new scene
- Set the camera as the observer
- Dynamically set the camera far clipping plane.
- Link a custom camera as the observer.
Upload a scene
To upload a scene, follow these steps:
- Log into the Digital Twin Dashboard.
Select New to create a scene.

Name the scene
My First Sceneand select Create.Select files to upload a file.

Create a cloud streaming behavior
To create a cloud streaming behavior, follow these steps:
- Open your Unity project.
- Go to the Assets folder in the Project window.
- Right Click and Select Create > C# Script.
- Rename the new script as
CloudDataStreaming. - Open the
CloudDataStreamingscript and replace the content with the following:using System; using System.Threading.Tasks; using Unity.Cloud.Common; using Unity.Cloud.Common.Runtime; using Unity.Cloud.DataStreaming.Runtime; using Unity.Cloud.Identity; using Unity.Cloud.Identity.Runtime; using Unity.Cloud.Storage; using UnityEngine; public class CloudDataStreaming : MonoBehaviour { [SerializeField] string m_SceneId; IServiceHostResolver m_ServiceHostResolver; UnityHttpClient m_HttpClient; CompositeAuthenticator m_Authenticator; ServiceHttpClient m_Service; DataStreamer m_Streamer; async Task Start() { try { m_ServiceHostResolver = UnityRuntimeServiceHostResolverFactory.Create(); m_HttpClient = new UnityHttpClient(); // Authenticate var authenticationPlatformSupport = PlatformSupportFactory.GetAuthenticationPlatformSupport(); var playerSettings = UnityCloudPlayerSettings.Instance; var compositeAuthenticatorSettings = new CompositeAuthenticatorSettingsBuilder(m_HttpClient, authenticationPlatformSupport, m_ServiceHostResolver) .AddDefaultPersonalAccessTokenProvider() .AddAuthenticator(new CommandLineAccessTokenProvider(authenticationPlatformSupport)) .AddDefaultPkceAuthenticator(playerSettings) .Build(); m_Authenticator = new CompositeAuthenticator(compositeAuthenticatorSettings); await m_Authenticator.InitializeAsync(); if (m_Authenticator.AuthenticationState == AuthenticationState.LoggedOut) await m_Authenticator.LoginAsync(); m_Service = new ServiceHttpClient(m_HttpClient, m_Authenticator, playerSettings); // Retrieving our uploaded scene. var sceneProvider = new SceneProvider(m_Service, m_ServiceHostResolver); var scene = await sceneProvider.GetSceneAsync(new SceneId(m_SceneId)); // Create the settings for our streamer. var builder = DataStreamerSettingsBuilder.CreateDefaultBuilder(); builder.SetScene(scene, m_Service, m_ServiceHostResolver); var settings = builder.Build(); // Start to stream. m_Streamer = new DataStreamer(); m_Streamer.Open(settings); } catch (Exception exception) { Debug.LogException(exception); } } void OnDestroy() { m_Streamer?.Close(); m_Authenticator?.Dispose(); } }
Set up a new scene
To set up a new scene, follow these steps:
- Create a new scene.
- Create a GameObject called Streamer.
- Select Add Component in the Inspector window and add the Cloud Data Streaming component.
- Copy the Scene ID part of the scene details on the Digital Twin Dashboard.
- Paste the Scene ID in the Cloud Data Streaming inspector field.
- Select Play.
- No geometry gets loaded.
- Select Stop.
Set the camera as the observer
As you saw in the previous steps, no meshes were loaded. This happened because the DataStreamer didn't know what to load since it's responsible for loading only what is required and it ignores geometry which is not in an observer's field of view. To fix this:
- Insert a m_CameraObserver field on your CloudDataStreaming Component.
public class CloudDataStreaming : MonoBehaviour { [SerializeField] Camera m_CameraObserver; [SerializeField] string m_SceneId; - Insert AddObserver call in the CloudDataStreaming.Start Method.
// Start to stream. m_Streamer = new DataStreamer(); var observer = SceneObserverFactory.CreateCameraObserver(m_CameraObserver); m_Streamer.AddObserver(observer); m_Streamer.Open(settings); - Add a Camera in your scene if one doesn't exist.
- Set the CloudDataStreaming Component Camera Observer value to your scene camera.
- Select Play to view your project load in front of the camera.1
- Select Stop.
Dynamically set the camera far clipping plane and automatically position it.
Now that the DataStreamer knows what to load based on the camera position, you might have seen display issues when geometry were too far from the camera. When moving the camera too far from the streamed asset, you will end up having the asset disappear since the camera clipping plane value is too low.
To prevent setting the far clipping plane to an arbitrary value, we will dynamically link its value to the distance between the camera and the asset.
- Go to the Assets folder in the Project window.
- Go to Add (+) > C# Script.
- Rename the new script as
CameraSetup. - Open the
CameraSetupscript and replace the content with the following:using UnityEngine; using Unity.Cloud.DataStreaming.Runtime; [RequireComponent(typeof(Camera))] public class CameraSetup : MonoBehaviour { CameraUtility m_CameraUtil; Bounds? m_MainBounds; void Start() { var camera = GetComponent<Camera>(); m_CameraUtil = new CameraUtility(camera); } public void SetView(Bounds? bounds) { if (bounds is not null) { m_MainBounds = bounds; m_CameraUtil.SetView(m_MainBounds.Value); } } void Update() { if (m_MainBounds is not null) { m_CameraUtil.SetClipPlane(m_MainBounds.Value); } } } - Insert a m_CameraSetup field on your CloudDataStreaming Component.
[SerializeField] Camera m_CameraObserver; [SerializeField] CameraSetup m_CameraSetup; [SerializeField] string m_SceneId; - Insert SetView call in the CloudDataStreaming.Start Method.
// Start to stream. m_Streamer = new DataStreamer(); var observer = SceneObserverFactory.CreateCameraObserver(m_CameraObserver); m_Streamer.AddObserver(observer); m_Streamer.Open(settings); var interest = await m_Streamer.GetDefaultVolumeOfInterestAsync(); m_CameraSetup.SetView(interest.Bounds); - Select the scene camera.
- Select Add Component in the Inspector window and add the CameraSetup component.
- Set the CameraSetup Component Camera value to your scene camera.
- Set the CloudDataStreaming Component CameraSetup value to the newly created CameraSetup component.
- Select Play to view your project load in front of the camera.
- The camera will initialize itself to a default position.
- Select the scene camera.
- Move the camera and look at the far clipping plane value getting updated.
1 If the scene does not load in front of the camera, select the menu item Edit / Frame Streamed Scene.