Use case | Access scenes
This use case outlines how to set up your project to access scenes.
Prerequisites
To accomplish this use case, you need to set up a Unity scene, create CloudWorkspaceRepository and StorageBehaviour as described in Use case | Manage workspaces.
Access scenes
- In your Unity project window, go to Assets > Scripts.
- Right-click the
Assets/Scriptsfolder. - Select Create > C# Script.
- Name your script
ScenesExamples. - Update the
ScenesExamplesclass to look like the following:using System; using System.Threading.Tasks; using System.Linq; using UnityEngine; class ScenesExamples { readonly IWorkspace m_Workspace; public ScenesExamples(IWorkspace workspace) { m_Workspace = workspace; } public async Task RunScenesExamples() { await ListScenesExample(); await GetSceneByIdExample(); } async Task ListScenesExample() { Debug.Log($"<color=green>List scenes example:</color>"); var scenes = await m_Workspace.ListScenesAsync(); Debug.Log($"Fetched scenes:"); foreach (var scene in scenes) { Debug.Log($"Scene ID: {scene.Id}, Scene Name: '{scene.Name}'"); } } async Task GetSceneByIdExample() { Debug.Log($"<color=green>Get scene by id example:</color>"); var scene = (await m_Workspace.ListScenesAsync()).FirstOrDefault(); if (scene != null) { var id = scene.Id; var fetchedScene = await m_Workspace.GetSceneAsync(id); Debug.Log($"Fetched scene:"); Debug.Log($"Scene ID: {fetchedScene.Id}, Scene Name: '{fetchedScene.Name}'"); } else { Debug.Log($"Workspace {m_Workspace} does not contain any scenes"); } } } - Replace
RunExamples()method in yourStorageBehaviourclass with following code:const string k_WorkspaceName = "<enter-workspace-name-here>"; async Task RunExamples() { var userProvider = PlatformServices.UserInfoProvider; var user = await userProvider.GetUserInfoAsync(); var organization = user.Organizations.Find(x => x.Name == k_OrganizationName); if (organization != null) { var workspaceExamples = new WorkspaceExamples(m_WorkspaceRepository, organization); await workspaceExamples.RunWorkspacesExamples(); var workspace = await workspaceExamples.GetOrCreateWorkspace(k_WorkspaceName); var scenesExamples = new ScenesExamples(workspace); await scenesExamples.RunScenesExamples(); } else { Debug.Log($"Organization {k_OrganizationName} was not found. Try different organization name"); } // Logs you out after running the examples if (PlatformServices.CompositeAuthenticator.RequiresGUI) await PlatformServices.CompositeAuthenticator.LogoutAsync(); } - Paste the name of a workspace that contains scenes into the
stringnamedk_WorkspaceName. If you don't know the workspace name, try to use your organization name.
Note: You can find your organization name in the Digital Twin Dashboard. Refer to Get started.
Run examples
- In the Unity Editor, select Play.
- Check your console to confirm that you have successfully requested and listed available scenes from the specified workspace.