Use case | Get workspace and scene information
This use case outlines how to set up your scene to retrieve workspace and scene information.
Prerequisites
To accomplish this use case, you require the following:
- Integrate authentication in your scene using the Digital Twins Identity package
Overview
To accomplish this use case, do the following:
- Instantiate a WorkspaceProvider in PlatformServices
- Write the SceneManager class to manage workspace and scene selection
- Optional | Display workspace and scene information
Instantiate a WorkspaceProvider in PlatformServices
To instantiate a WorkspaceProvider in the PlatformServices class, follow these steps:
Add the following references in the
PlatformServicesclass:- A public reference to
IWorkspaceProvider A private reference to the
WorkspaceProviderimplementationstatic WorkspaceProvider s_WorkspaceProvider; public static IWorkspaceProvider WorkspaceProvider => s_WorkspaceProvider;
- A public reference to
Initialize the services in the
InitializeAsyncmethod. The Get user information guide includes definitions of thes_ServiceHttpClientands_CloudConfigurationvariables.public static async Task InitializeAsync() { // ... s_WorkspaceProvider = new WorkspaceProvider(s_ServiceHttpClient, s_CloudConfiguration); // ... }Shutdown the services in the
Shutdownmethod.public static void ShutDownServices() { // ... s_WorkspaceProvider = null; // ... }
Write the SceneManager class to manage workspace and scene selection
To write the SceneManager class so it manages workspace and scene selection, follow these steps:
Create two dropdowns in your scene, one to select a workspace and one to select a scene.

Create a
SceneManagerscript and attach it to theSceneManagerGameObject. This script manages the workspace and scene that the user selects.Update the
SceneManagerclass to contain the following:- References to the dropdowns
- References to the
IAuthenticatorandIWorkspaceProviderservices - References to the lists of workspaces and scenes
Two events that are raised when the user selects a different workspace or scene
public class SceneManager : MonoBehaviour { [SerializeField] Dropdown m_WorkspaceDropdown; [SerializeField] Dropdown m_SceneDropdown; IAuthenticator m_Authenticator; IWorkspaceProvider m_WorkspaceProvider; List<IWorkspace> m_Workspaces; List<IScene> m_Scenes; public event Action<IWorkspace> WorkspaceSelected; public event Action<IScene> SceneSelected; }
Implement the
Awakemethod so it retrieves services from thePlatformServices. This method can then subscribe (and unsubscribe inOnDestroy) to different events.void Awake() { m_Authenticator = PlatformServices.Authenticator; m_WorkspaceProvider = PlatformServices.WorkspaceProvider; m_Authenticator.AuthenticationStateChanged += OnAuthenticationStateChanged; m_WorkspaceDropdown.onValueChanged.AddListener(new UnityEngine.Events.UnityAction<int>(ApplyWorkspaceUpdate)); m_SceneDropdown.onValueChanged.AddListener(new UnityEngine.Events.UnityAction<int>(ApplySceneUpdate)); } void OnDestroy() { m_Authenticator.AuthenticationStateChanged -= OnAuthenticationStateChanged; m_WorkspaceDropdown.onValueChanged.RemoveAllListeners(); m_SceneDropdown.onValueChanged.RemoveAllListeners(); }Implement the
OnAuthenticationStateChangedmethod so that if the user is logged out, the workspaces are emptied and both events receive a null value. Otherwise, the workspaces are populated.async void OnAuthenticationStateChanged(AuthenticationState state) { if (state == AuthenticationState.LoggedIn) { await PopulateWorkspaces(); } else { m_WorkspaceDropdown.ClearOptions(); m_SceneDropdown.ClearOptions(); m_WorkspaceDropdown.interactable = false; m_SceneDropdown.interactable = false; WorkspaceSelected?.Invoke(null); SceneSelected?.Invoke(null); } }Implement the
PopulateWorkspacesmethod so that it goes through the available workspaces in a loop (due to theIWorkspaceProvider) and fills the dropdown with their names. Make the dropdown interactive when there are workspaces available, and call theApplyWorkspaceUpdatemethod to instantly update the UI.async Task PopulateWorkspaces() { m_WorkspaceDropdown.ClearOptions(); var list = new List<Dropdown.OptionData>(); m_Workspaces = (await m_WorkspaceProvider.ListWorkspacesAsync()).ToList(); foreach (var workspace in m_Workspaces) { list.Add(new Dropdown.OptionData(workspace.Name)); } if (list.Count > 0) { m_WorkspaceDropdown.AddOptions(list); m_WorkspaceDropdown.interactable = true; ApplyWorkspaceUpdate(m_WorkspaceDropdown.value); } else { m_WorkspaceDropdown.interactable = false; WorkspaceSelected?.Invoke(null); } }Implement the
ApplyWorkspaceUpdatemethod so it raises theWorkspaceSelectedevent and launches thePopulateScenesprocess.async void ApplyWorkspaceUpdate(int value) { var workspace = m_Workspaces[value]; WorkspaceSelected?.Invoke(workspace); await PopulateScenes(workspace); }Implement the
PopulateScenemethod, which is similar to thePopulateWorkspacesmethod but requires anIWorkspaceargument to call itsListScenesAsyncmethod.async Task PopulateScenes(IWorkspace workspace) { m_SceneDropdown.ClearOptions(); var list = new List<Dropdown.OptionData>(); m_Scenes = (await workspace.ListScenesAsync()).ToList(); foreach (var scene in m_Scenes) { list.Add(new Dropdown.OptionData(scene.Name)); } if (list.Count > 0) { m_SceneDropdown.AddOptions(list); m_SceneDropdown.interactable = true; ApplySceneUpdate(m_SceneDropdown.value); } else { m_SceneDropdown.interactable = false; SceneSelected?.Invoke(null); } }Implement the
ApplySceneUpdatemethod so it raises theSceneSelectedevent.void ApplySceneUpdate(int value) { var scene = m_Scenes[value]; SceneSelected?.Invoke(scene); }
Optional | Display workspace and scene information
To display workspace and scene information in the screen, follow these steps:
Create two large
Textfields in your scene that display information about the selected workspace and scene.
Create a
SceneInfoUpdaterscript and attach it to theSceneManagerGameObject. This script subscribes to the workspace and scene events and updates theTextfields to display information about the selected workspace and scene.Update the
SceneInfoUpdaterclass so it references yourTextfields and theSceneManagerGameObject.public class SceneInfoUpdater : MonoBehaviour { [SerializeField] SceneManager m_SceneManager; [SerializeField] Text m_WorkspaceText; [SerializeField] Text m_SceneText; }Implement the
Awakemethod so it subscribes to theSceneManager's events and theOnDestroymethod unsubscribes from theSceneManager's events.void Awake() { m_SceneManager.WorkspaceSelected += OnWorkspaceSelected; m_SceneManager.SceneSelected += OnSceneSelected; } void OnDestroy() { m_SceneManager.WorkspaceSelected -= OnWorkspaceSelected; m_SceneManager.SceneSelected -= OnSceneSelected; }Implement
OnWorkspaceSelectedandOnSceneSelectedso they update theTextfield to display the ID of the selected workspace and scene.void OnWorkspaceSelected(IWorkspace workspace) { m_WorkspaceText.text = workspace == null ? "No workspace selected" : workspace.Id.ToString(); } void OnSceneSelected(IScene scene) { m_SceneText.text = scene == null ? "No scene selected" : scene.Id.ToString(); }