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:
- Open the
AnnotationBehaviourscript you created in Get started. Paste the following code sample into your script. The new
CreateTopicExample()method creates aTopicCreationrequest object and displays information about the created topic. TheCreateTopicExample()method is called in theRunExamples()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}"); } }Run the scene and check your console to confirm that the new topic was created and fetched.
