docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use case: Link the Camera to the DataStreamer

    This use case describes the how to set up a camera in a streaming Unity Editor scene. Use this use case to learn how to set the camera observer on a DataStreamer and dynamically set the Far Clipping Plane for it.

    To accomplish this use case, do the following:

    1. Upload an asset
    2. Create a cloud streaming behavior
    3. Set up a new scene
    4. Set the camera as the observer
    5. Dynamically set the camera far clipping plane.
    6. Link a custom camera as the observer.

    Before you start

    Before you start, install the following Unity Cloud package dependency from the registry by name:

    • unity.cloud.identity

    For more information on how to install packages by name, see Installing a package by name

    How do I...?

    Upload an asset

    To upload an asset, follow these steps:

    1. Log into the Asset Manager Dashboard.

    2. Select Add asset to create a new asset.

      Screen capture that displays adding a new asset

    3. Name the asset My Asset.

    4. Select files to upload.

      Screen capture that displays uploading files for your asset

    5. Select Publish asset to finalize.

      Screen capture that displays publishing your asset

    Create a cloud streaming behavior

    To create a cloud streaming behavior, follow these steps:

    1. Open your Unity Editor Project.
    2. Go to the Assets folder in the Project window.
    3. Select and hold Create.
    4. Select C# Script.
    5. Rename the new script as CloudDataStreaming.
    6. Open the CloudDataStreaming script and replace the content with the following:
      using System;
      using System.Threading;
      using System.Threading.Tasks;
      using Unity.Cloud.AppLinking.Runtime;
      using Unity.Cloud.Assets;
      using Unity.Cloud.Common;
      using Unity.Cloud.Common.Runtime;
      using Unity.Cloud.DataStreaming.Runtime;
      using Unity.Cloud.DataStreaming.Runtime.AssetManager;
      using Unity.Cloud.Identity;
      using Unity.Cloud.Identity.Runtime;
      using UnityEngine;
      
      public class CloudDataStreaming : MonoBehaviour
      {
          [SerializeField]
          string m_OrganizationId;
      
          [SerializeField]
          string m_ProjectId;
      
          [SerializeField]
          string m_AssetId;
      
          [SerializeField]
          string m_AssetVersion;
      
          IServiceHostResolver m_ServiceHostResolver;
          UnityHttpClient m_HttpClient;
          CompositeAuthenticator m_Authenticator;
          ServiceHttpClient m_Service;
          IDataStreamer 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,
                          playerSettings)
                      .AddDefaultBrowserAuthenticatedAccessTokenProvider(playerSettings)
                      .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);
      
                  // Get the asset to load.
                  var assetRepository = AssetRepositoryFactory.Create(
                      m_Service,
                      m_ServiceHostResolver);
      
                  var projectDescriptor = new ProjectDescriptor(
                      new OrganizationId(m_OrganizationId),
                      new ProjectId(m_ProjectId));
      
                  var assetDescriptor = new AssetDescriptor(
                      projectDescriptor,
                      new AssetId(m_AssetId),
                      new AssetVersion(m_AssetVersion));
      
                  var asset = await assetRepository.GetAssetAsync(
                      assetDescriptor,
                      CancellationToken.None);
      
                  // Create the settings for our streamer.
                  var settings = DataStreamerSettingsBuilder
                      .CreateDefaultBuilder()
                      .Build();
      
                  // Start to stream.
                  m_Streamer = IDataStreamer.Create();
                  var stage = m_Streamer.Open(settings);
                  stage.Models.Add(x => x.FromAsset(asset, m_Service));
              }
              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:

    1. Create a new scene.
    2. Create a GameObject called Streamer.
    3. Select Add Component in the Inspector window and add the Cloud Data Streaming component.
    4. Copy the Organization ID, Project ID, Asset ID and Asset Version part of the Asset Manager Dashboard URL. The format is the following: https://cloud.unity.com/home/organizations/<Organization ID>/projects/<Project ID>/assets?assetId=<Asset ID>:<Asset Version>
    5. Paste the Organization ID, Project ID, Asset ID and Asset Version in the corresponding Cloud Data Streaming inspector fields.
    6. Select Play.
    7. No geometry gets loaded.
    8. 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:

    1. Insert a m_CameraObserver field on your CloudDataStreaming component.
      public class CloudDataStreaming : MonoBehaviour
      {
          [SerializeField]
          Camera m_CameraObserver;
      
          [SerializeField]
          string m_OrganizationId;
      
          [SerializeField]
          string m_ProjectId;
      
          [SerializeField]
          string m_AssetId;
      
          [SerializeField]
          string m_AssetVersion;
      
          IDataStreamer m_Streamer;
      
    2. Insert AddObserver call in the CloudDataStreaming.Start method.
      // Start to stream.
      m_Streamer = IDataStreamer.Create();
      var stage = m_Streamer.Open(settings);
      
      var observer = StageObserverFactory.CreateCameraObserver(m_CameraObserver);
      stage.Observers.Add(observer);
      stage.Models.Add(x => x.FromAsset(asset, m_Service));
      
    3. Add a Camera in your scene if one doesn't exist.
    4. Set the CloudDataStreaming component Camera Observer value to your scene camera.
    5. Select Play to view your model load in front of the camera.1
    6. Select Stop.

    Dynamically set the camera far clipping plane and automatically position it

    After the DataStreamer detects what to load based on the camera position, you may notice display issues when geometry was too far from the camera. When you move the camera too far from the streamed asset, the model can disappear when 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 model.

    1. Go to the Assets folder in the Project window.
    2. Go to Add (+) > C# Script.
    3. Rename the new script as CameraSetup.
    4. Open the CameraSetup script and replace the content with the following:
      using UnityEngine;
      using Unity.Cloud.DataStreaming.Runtime;
      using Unity.Cloud.HighPrecision.Runtime;
      using Unity.Cloud.DataStreaming.Runtime.AssetManager;
      
      [RequireComponent(typeof(Camera))]
      public class CameraSetup : MonoBehaviour
      {
          CameraUtility m_CameraUtil;
          DoubleBounds? m_MainBounds;
      
          void Start()
          {
              var camera = GetComponent<Camera>();
              m_CameraUtil = new CameraUtility(camera);
          }
      
          public void SetView(DoubleBounds? 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);
              }
          }
      }
      
    5. Insert a m_CameraSetup field on your CloudDataStreaming Component.
      [SerializeField]
      Camera m_CameraObserver;
      
      [SerializeField]
      CameraSetup m_CameraSetup;
      
      [SerializeField]
      string m_OrganizationId;
      
      [SerializeField]
      string m_ProjectId;
      
      [SerializeField]
      string m_AssetId;
      
      [SerializeField]
      string m_AssetVersion;
      
      IDataStreamer m_Streamer;
      
    6. Insert SetView call in the CloudDataStreaming.Start Method.
      // Start to stream.
      m_Streamer = IDataStreamer.Create();
      var stage = m_Streamer.Open(settings);
      
      stage.Models.Add(x => x.FromAsset(asset, m_Service));
      
      var bounds = await stage.GetWorldBoundsAsync();
      m_CameraSetup.SetView(bounds);
      
      var observer = StageObserverFactory.CreateCameraObserver(m_CameraObserver);
      stage.Observers.Add(observer);
      
    7. Select the scene camera.
    8. Select Add Component in the Inspector window and add the CameraSetup component.
    9. Set the CloudDataStreaming component CameraSetup value to the newly created CameraSetup component.
    10. Select Play to view your asset load in front of the camera.
    11. The camera will initialize itself to a default position.
    12. Select the scene camera.
    13. Move the camera and look at the far clipping plane value getting updated.

    1 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.

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)