Use case: Load an Asset 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 an asset from the cloud.
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
Create a cloud streaming behavior
To create a cloud streaming behavior, follow these steps:
- Open your Unity Project.
- In the Project window, select and right click on the Assets folder.
- Select Create > C# Script.
- Rename the new script as
CloudDataStreaming
. - Open the
CloudDataStreaming
script and replace the content with the following:using System; using System.Threading; using System.Threading.Tasks; 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 Unity.Cloud.AppLinking.Runtime; using UnityEngine; public class CloudDataStreaming : MonoBehaviour { [SerializeField] string m_OrganizationId; [SerializeField] string m_ProjectId; [SerializeField] string m_AssetId; [SerializeField] string m_AssetVersion = "1"; [SerializeField] Camera m_Camera; [SerializeField] float m_ScreenSpaceError = StageObserverFactory.DefaultScreenSpaceError; 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 observer = StageObserverFactory.CreateCameraObserver( m_Camera, m_ScreenSpaceError); var stage = m_Streamer.Open(settings); stage.Models.Add(x => x.FromAsset(asset, m_Service)); stage.Observers.Add(observer); } 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 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>
- Paste the Organization ID, Project ID, Asset ID and Asset Version in the corresponding Cloud Data Streaming inspector fields.
- Add a Camera in your scene if one doesn't exist.
- Set the CloudDataStreaming Component Camera value to your scene camera.
- Select Play to view your asset load in front of the camera.1
1 If the asset does not load in front of the camera, select the menu item Edit / Frame Streamed Model
to frame the asset in the Unity Scene View.