docs.unity3d.com
  • Manual
  • Scripting API
  • Changelog
  • License

    • Storage
      • Installation
      • Getting started
    • Use Cases
      • Getting workspace and scene information
    • Samples
      • Getting workspace and scene information

    Use case : Getting Workspace and Scene information

    This guide explains how to set up your scene in order to get workspace and scene information.

    Prerequisites

    • Follow Identity's Integrating authentication in your scene guide.

    Overview

    In order to retrieve workspace and scene information, perform the following procedures:

    1. Instantiate a WorkspaceProvider in PlatformServices
    2. Write the SceneManager class to handle selection of workspaces and scenes
    3. Display workspace and scene information in the screen (optional)

    Instantiate a WorkspaceProvider in PlatformServices

    1. In the PlatformServices class, add a public reference to IWorkspaceProvider, and a private reference to the WorkspaceProvider implementation.

       static WorkspaceProvider s_WorkspaceProvider;
       public static IWorkspaceProvider WorkspaceProvider => s_WorkspaceProvider;
      
    2. Initialize the services properly in the InitializeAsync method. The variable provided in the constructor have been defined in Identity's Getting user information guide.

       public static async Task InitializeAsync()
       {
           // ...
      
           s_WorkspaceProvider = new WorkspaceProvider(s_ServiceHttpClient, s_CloudConfiguration);
      
           // ...
       }
      
    3. Shutdown the services properly in the Shutdown method.

       public static void ShutDownServices()
       {
           // ...
      
           s_WorkspaceProvider = null;
      
           // ...
       }
      

    Write the SceneManager class to handle selection of workspaces and scenes

    1. In your scene, create two dropdowns, whose purpose is to select a worskpace and a scene. Creating a text field in the scene

    2. Create a new SceneManager script, and attach it to the SceneManager GameObject. This script will be responsible of managing workspace and scene have been selected by the user.

    3. The SceneManager class should contain :

      • References to the dropdowns
      • References to IAuthenticator and IWorkspaceProvider services
      • References to the lists of Workspaces and Scenes
      • Two events that get raised whenever 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. Next, the Awake method should retrieve the services from PlatformServices. Then, it can subscribe to different events (which need to be unsubscribed in OnDestroy).

       void Awake()
       {
           m_Authenticator = PlatformServices.InteractiveAuthenticator;
           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. Then, we implement the OnAuthenticationStateChanged method : if the user is logged out, we empty the workspaces and send null to both events. Otherwise, we'll start by populating the workspaces.

       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. We simply need to iterate over the available workspaces (thanks to the IWorkspaceProvider) and fill the dropdown with their different names. Based on the number of available workspaces, we make the dropdown interactable or not. We also call ApplyWorkspaceUpdate to update the UI instantly.

       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. The ApplyWorkspaceUpdate method 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 very similar to PopulateWorkspace. The only difference is that we need 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. The ApplySceneUpdate method raises the SceneSelected event.

       void ApplySceneUpdate(int value)
       {
           var scene = m_Scenes[value];
           SceneSelected?.Invoke(scene);
       }
      

    Display workspace and scene information in the screen (optional)

    1. In your scene, create two (large) Text fields, whose purpose is to display information about the selected worskpace and scene. Creating a text field in the scene

    2. Create a new SceneInfoUpdater script, and attach it to the SceneManager GameObject. This script will be responsible hooking to the workspace and scene selected events, and updating the text to show information.

    3. The SceneInfoUpdater class should contain :

      • References to the text fields
      • A reference to the SceneManager created above

        public class SceneInfoUpdater : MonoBehaviour
        {
           [SerializeField]
           SceneManager m_SceneManager;
        
           [SerializeField]
           Text m_WorkspaceText;
        
           [SerializeField]
           Text m_SceneText;
        }
        
    4. Next, the Awake method should simply subscribe to SceneManager's events ; similarly, the OnDestroy method should unsubcribe.

       void Awake()
       {
           m_SceneManager.WorkspaceSelected += OnWorkspaceSelected;
           m_SceneManager.SceneSelected += OnSceneSelected;
       }
      
       void OnDestroy()
       {
           m_SceneManager.WorkspaceSelected -= OnWorkspaceSelected;
           m_SceneManager.SceneSelected -= OnSceneSelected;
       }
      
    5. Finally, we can implement OnWorkspaceSelected and OnSceneSelected to update the info accordingly.

       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();
       }
      
    Back to top Generated by DocFX
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023