docs.unity3d.com
    Show / Hide Table of Contents

    Leave comments on topics

    You can use the Annotations package to create a topic with an initial comment, leave comments on existing topics, and fetch comments from specific topics.

    Create a topic with an initial comment

    To create a topic with an initial comment, follow these steps:

    1. Open the AnnotationBehaviour script you created in Get started.
    2. Paste the following code sample into your script:
      using System;
      using System.Linq;
      using System.Threading.Tasks;
      using Unity.Cloud.Annotation;
      using Unity.Cloud.Annotation.Runtime;
      using Unity.Cloud.Common;
      using Unity.Cloud.Identity;
      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 CreateTopicWithInitialCommentExample();
              await CreateCommentOnTopicExample();
      
              // Logs you out after running the examples
              if (PlatformServices.CompositeAuthenticator.RequiresGUI)
                  await PlatformServices.CompositeAuthenticator.LogoutAsync();
          }
      
          // Create a topic with an initial comment associated to it
          async Task CreateTopicWithInitialCommentExample()
          {
              // Creates a TopicCreation request object using a TopicCreationBuilder and populates it with the necessary fields
              var topicCreationBuilder = new TopicCreationBuilder();
              topicCreationBuilder.SetTitle("A topic with a comment");
              topicCreationBuilder.SetDescription("This topic has an initial comment");
              topicCreationBuilder.SetInitialComment("This is the initial comment");
      
              // 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}");
      
              // Retrieves all the comments on the created topic
              var comments = await createdTopic.GetCommentsAsync();
      
              // Outputs the information for each comment
              foreach (var comment in comments)
              {
                  Debug.Log($"Comment ID: {comment.Id}, Comment text: {comment.Text}");
              }
          }
      
          // Add a comment to an existing topic
          async Task CreateCommentOnTopicExample()
          {
              // Creates a TopicCreation request object using a TopicCreationBuilder and populates it with the necessary fields
              var topicCreationBuilder = new TopicCreationBuilder();
              topicCreationBuilder.SetTitle("A topic on which to comment");
              topicCreationBuilder.SetDescription("This topic will be commented on");
      
              // 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}");
      
              // Creates a CommentCreation object using a CommentCreationBuilder
              var commentCreationBuilder = new CommentCreationBuilder();
              commentCreationBuilder.SetText("A new comment");
      
              // Sends the request to the topic you want to comment on
              var createdComment = await createdTopic.CreateCommentAsync(commentCreationBuilder.GetCommentCreation());
      
              // Outputs the information about the created comment
              Debug.Log($"Comment ID: {createdComment.Id}, Comment text: {createdComment.Text}");
      
              // Retrieves all the comments on the created topic
              var comments = await createdTopic.GetCommentsAsync();
      
              // Outputs the information for each comment
              foreach (var comment in comments)
              {
                  Debug.Log($"Comment From Topic ID: {comment.Id}, Comment From Topic text: {comment.Text}");
              }
          }
      }
      
    3. Check your console to confirm that the new topics and comments were created.
      Screenshot of the console after successful topic creation with initial comment
      Screenshot of the console after successful comment creation
    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