docs.unity3d.com

    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:

    1. Instantiate a WorkspaceProvider in PlatformServices
    2. Write the SceneManager class to manage workspace and scene selection
    3. Optional | Display workspace and scene information

    Instantiate a WorkspaceProvider in PlatformServices

    To instantiate a WorkspaceProvider in the PlatformServices class, follow these steps:

    1. Add the following references in the PlatformServices class:

      • A public reference to IWorkspaceProvider
      • A private reference to the WorkspaceProvider implementation

           static WorkspaceProvider s_WorkspaceProvider;
           public static IWorkspaceProvider WorkspaceProvider => s_WorkspaceProvider;
        
    2. Initialize the services in the InitializeAsync method. The Get user information guide includes definitions of the s_ServiceHttpClient and s_CloudConfiguration variables.

          public static async Task InitializeAsync()
          {
              // ...
              s_WorkspaceProvider = new WorkspaceProvider(s_ServiceHttpClient, s_CloudConfiguration);
              // ...
          }
      
    3. 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:

    1. Create two dropdowns in your scene, one to select a workspace and one to select a scene. Screenshot of two dropdowns in a scene

    2. Create a SceneManager script and attach it to the SceneManager GameObject. This script manages the workspace and scene that the user selects.

    3. Update the SceneManager class to contain the following:

      • References to the dropdowns
      • References to the IAuthenticator and IWorkspaceProvider 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;
           }
        
    4. Implement the Awake method so it retrieves services from the PlatformServices. This method can then subscribe (and unsubscribe in OnDestroy) 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();
          }
      
    5. 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);
              }
          }
      
    6. Implement the PopulateWorkspaces method so that it goes through the available workspaces in a loop (due to the IWorkspaceProvider) and fills the dropdown with their names. Make the dropdown interactive when there are workspaces available, and call the ApplyWorkspaceUpdate 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);
              }
          }
      
    7. Implement the ApplyWorkspaceUpdate method so it raises the WorkspaceSelected event and launches the PopulateScenes process.

          async void ApplyWorkspaceUpdate(int value)
          {
              var workspace = m_Workspaces[value];
              WorkspaceSelected?.Invoke(workspace);
              await PopulateScenes(workspace);
          }
      
    8. Implement the PopulateScene method, which is similar to the PopulateWorkspaces method but requires an IWorkspace argument to call its ListScenesAsync 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);
              }
          }
      
    9. Implement the ApplySceneUpdate method so it raises the SceneSelected 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:

    1. Create two large Text fields in your scene that display information about the selected workspace and scene. Screenshot of workspace and scene information text fields in a scene

    2. Create a SceneInfoUpdater script and attach it to the SceneManager GameObject. This script subscribes to the workspace and scene events and updates the Text fields to display information about the selected workspace and scene.

    3. Update the SceneInfoUpdater class so it references your Text fields and the SceneManager GameObject.

          public class SceneInfoUpdater : MonoBehaviour
          {
              [SerializeField]
              SceneManager m_SceneManager;
              [SerializeField]
              Text m_WorkspaceText;
              [SerializeField]
              Text m_SceneText;
          }
      
    4. Implement the Awake method so it subscribes to the SceneManager's events and the OnDestroy method unsubscribes from the SceneManager's events.

          void Awake()
          {
              m_SceneManager.WorkspaceSelected += OnWorkspaceSelected;
              m_SceneManager.SceneSelected += OnSceneSelected;
          }
          void OnDestroy()
          {
              m_SceneManager.WorkspaceSelected -= OnWorkspaceSelected;
              m_SceneManager.SceneSelected -= OnSceneSelected;
          }
      
    5. Implement OnWorkspaceSelected and OnSceneSelected so they update the Text 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();
          }
      
    Copyright © Unity Technologies
    Generated by DocFX
    on
    Terms of use