docs.unity3d.com
    Show / Hide Table of Contents

    Use case: Load a scene from the cloud

    This use case outlines the basics of setting up a streaming-ready scene. Use this use case to learn how to stream a scene from the cloud.

    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:

    1. Upload a scene
    2. Create a cloud streaming behavior
    3. Set up a new scene

    Upload a scene

    To upload a scene, follow these steps:

    1. Log into the Digital Twin Dashboard.
    2. Select New to create a scene.

      Screen capture that displays adding a new scene

    3. Name the scene My First Scene and select Create.

    4. Select files to upload a file.

      Screen capture that displays uploading a file for your scene

    Create a cloud streaming behavior

    To create a cloud streaming behavior, follow these steps:

    1. Open your Unity project.
    2. Go to the Assets folder in the Project window.
    3. Right Click and Select Create > C# Script.
    4. Rename the new script as CloudDataStreaming.
    5. Open the CloudDataStreaming script 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;
      
          [SerializeField]
          Camera m_Camera;
      
          [SerializeField]
          float m_ScreenSpaceError = SceneObserverFactory.DefaultScreenSpaceError;
      
          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 pkceAuthenticatorSettingsBuilder = new PkceAuthenticatorSettingsBuilder(authenticationPlatformSupport, m_ServiceHostResolver);
                  pkceAuthenticatorSettingsBuilder.AddDefaultConfigurationProviderAndRequestHandler(m_HttpClient, playerSettings)
                      .AddDefaultAccessTokenExchanger(m_HttpClient);
      
                  var compositeAuthenticatorSettings = new CompositeAuthenticatorSettingsBuilder(m_HttpClient, authenticationPlatformSupport, m_ServiceHostResolver)
                      .AddDefaultPersonalAccessTokenProvider()
                      .AddAuthenticator(new CommandLineAccessTokenProvider(pkceAuthenticatorSettingsBuilder.Build()))
                      .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();
      
                  var observer = SceneObserverFactory.CreateCameraObserver(m_Camera, m_ScreenSpaceError);
                  m_Streamer.AddObserver(observer);
      
                  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:

    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 scene id part of the scene details on the Digital Twin Dashboard.
    5. Paste the Scene ID in the Cloud Data Streaming inspector field.
    6. Add a Camera in your scene if one doesn't exist.
    7. Set the CloudDataStreaming Component Camera value to your scene camera.
    8. Select Play to view your project load in front of the camera.1

    1 If the scene does not load in front of the camera, select the menu item Edit / Frame Streamed Scene.

    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023