docs.unity3d.com
    Show / Hide Table of Contents

    Create topics

    You can use the Annotations package to create new topics and fetch existing topics.

    Prerequisites

    Before you create a topic, you must first set up a Unity scene with an AnnotationRepository. See Get started for more information.

    Create a new topic

    To create a new topic, follow these steps:

    1. Open the AnnotationBehaviour script you created in Get started.
    2. Paste the following code sample into your script. The new CreateTopicExample() method creates a TopicCreation request object and displays information about the created topic. The CreateTopicExample() method is called in the RunExamples() method.

      using System;
      using System.Linq;
      using System.Threading.Tasks;
      using Unity.Cloud.Common;
      using Unity.Cloud.Identity;
      using Unity.Cloud.Annotation.Runtime;
      using UnityEngine;
      
      public class AnnotationBehaviour : MonoBehaviour
      {
          // The annotation repository retrieves, creates, edits, and removes annotations from a scene
          IAnnotationRepository m_AnnotationRepository;
      
          // The id for the scene to initialize the annotation repository with
          static readonly SceneId k_SceneId = new("<enter-id-here>");
      
          // The authenticator responsible for user login
          ICompositeAuthenticator m_CompositeAuthenticator;
          bool m_HasAttemptedLogin;
      
          async Task Start()
          {
              // Register to get notified of the login state after the authenticator has initialized
              m_CompositeAuthenticator = PlatformServices.CompositeAuthenticator;
              m_CompositeAuthenticator.AuthenticationStateChanged += ApplyAuthenticationState;
      
              // Apply the initial state for the authenticator
              ApplyAuthenticationState(m_CompositeAuthenticator.AuthenticationState);
          }
      
          void ApplyAuthenticationState(AuthenticationState newAuthenticationState)
          {
              switch (newAuthenticationState)
              {
                  case AuthenticationState.LoggedIn:
                      // If the user is logged in, initialize the repository
                      m_CompositeAuthenticator.AuthenticationStateChanged -= ApplyAuthenticationState;
                      _ = InitializeAnnotationRepository();
                      break;
                  case AuthenticationState.LoggedOut:
                      // If the user is logged out, attempt to automatically login
                      // Set a flag to ensure we only attempt an auto-login once
                      if (!m_HasAttemptedLogin)
                      {
                          m_HasAttemptedLogin = true;
                          _ = m_CompositeAuthenticator.LoginAsync();
                      }
                      else
                      {
                          // Unregister if login attempt failed
                          m_CompositeAuthenticator.AuthenticationStateChanged -= ApplyAuthenticationState;
                      }
                      break;
              }
          }
      
          async Task InitializeAnnotationRepository()
          {
              // Gets a scene
              var scene = await PlatformServices.SceneProvider.GetSceneAsync(k_SceneId);
      
              // Create a new AnnotationRepository with the chosen scene
              m_AnnotationRepository = new AnnotationRepository(scene, PlatformServices.ServiceHttpClient, PlatformServices.ServiceHostResolver);
      
              await RunExamples();
          }
      
          async Task RunExamples()
          {
              await CreateTopicExample();
      
              // Logs you out after running the examples
              if (PlatformServices.CompositeAuthenticator.RequiresGUI)
                  await PlatformServices.CompositeAuthenticator.LogoutAsync();
          }
      
          async Task CreateTopicExample()
          {
              // Creates a TopicCreation request object using a TopicCreationBuilder and populates it with the necessary fields
              var topicCreationBuilder = new TopicCreationBuilder();
              topicCreationBuilder.SetTitle("A new title");
              topicCreationBuilder.SetDescription("A descriptive description");
      
              // Sends the request to the AnnotationRepository and waits for the result
              var createdTopic = await m_AnnotationRepository.CreateTopicAsync(topicCreationBuilder.GetTopicCreation());
      
              // Outputs the created topic information
              Debug.Log($"Created Topic ID: {createdTopic.Id}, Created Topic Title: {createdTopic.Title}");
      
              // Fetches an existing topic using that existing topic's ID
              var fetchedTopic = await m_AnnotationRepository.GetTopicAsync(createdTopic.Id);
      
              // Outputs the fetched topic information
              Debug.Log($"Fetched Topic ID: {fetchedTopic.Id}, Created Topic Title: {fetchedTopic.Title}");
          }
      }
      
    3. Run the scene and check your console to confirm that the new topic was created and fetched.
      Screenshot of the console after successful topic fetching

    Back to top
    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