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
PlatformServices
class:- A public reference to
IWorkspaceProvider
A private reference to the
WorkspaceProvider
implementationstatic WorkspaceProvider s_WorkspaceProvider; public static IWorkspaceProvider WorkspaceProvider => s_WorkspaceProvider;
- A public reference to
Initialize the services in the
InitializeAsync
method. The Get user information guide includes definitions of thes_ServiceHttpClient
ands_CloudConfiguration
variables.public static async Task InitializeAsync() { // ... s_WorkspaceProvider = new WorkspaceProvider(s_ServiceHttpClient, s_CloudConfiguration); // ... }
Shutdown the services in the
Shutdown
method.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
SceneManager
script and attach it to theSceneManager
GameObject. This script manages the workspace and scene that the user selects.Update the
SceneManager
class to contain the following:- References to the dropdowns
- References to the
IAuthenticator
andIWorkspaceProvider
services - 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
Awake
method 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
OnAuthenticationStateChanged
method 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
PopulateWorkspaces
method 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 theApplyWorkspaceUpdate
method 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
ApplyWorkspaceUpdate
method so it raises theWorkspaceSelected
event and launches thePopulateScenes
process.async void ApplyWorkspaceUpdate(int value) { var workspace = m_Workspaces[value]; WorkspaceSelected?.Invoke(workspace); await PopulateScenes(workspace); }
Implement the
PopulateScene
method, which is similar to thePopulateWorkspaces
method but requires anIWorkspace
argument to call itsListScenesAsync
method.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
ApplySceneUpdate
method so it raises theSceneSelected
event.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
Text
fields in your scene that display information about the selected workspace and scene.Create a
SceneInfoUpdater
script and attach it to theSceneManager
GameObject. This script subscribes to the workspace and scene events and updates theText
fields to display information about the selected workspace and scene.Update the
SceneInfoUpdater
class so it references yourText
fields and theSceneManager
GameObject.public class SceneInfoUpdater : MonoBehaviour { [SerializeField] SceneManager m_SceneManager; [SerializeField] Text m_WorkspaceText; [SerializeField] Text m_SceneText; }
Implement the
Awake
method so it subscribes to theSceneManager
's events and theOnDestroy
method 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
OnWorkspaceSelected
andOnSceneSelected
so they update theText
field 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(); }