Edit topics and comments
You can use the Annotations package to make edits to existing topics and comments.
Edit existing topics and comments
To edit an existing topic, follow these steps:
- Open the
AnnotationBehaviourscript you created in Get started. 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 EditTopicExample(); await EditCommentExample(); // Logs you out after running the examples if (PlatformServices.CompositeAuthenticator.RequiresGUI) await PlatformServices.CompositeAuthenticator.LogoutAsync(); } // Create a topic with a specific title async Task<ITopic> CreateTopic(string title) { // Creates a TopicCreation request object using a TopicCreationBuilder and populates it with the necessary fields var topicCreationBuilder = new TopicCreationBuilder(); topicCreationBuilder.SetTitle(title); // Creates and retrieves the new topic var createdTopic = await m_AnnotationRepository.CreateTopicAsync(topicCreationBuilder.GetTopicCreation()); // Returns the new topic return createdTopic; } // Edit a topic by setting a new title async Task<ITopic> EditTopic(ITopic topicToEdit, string newTitle) { // Creates a TopicUpdate request object using a TopicUpdateBuilder and populates it with the fields of the edited topic var topicUpdateBuilder = new TopicUpdateBuilder(topicToEdit); topicUpdateBuilder.SetTitle(newTitle); // Updates and retrieves the topic var editedTopic = await m_AnnotationRepository.UpdateTopicAsync(topicUpdateBuilder.GetTopicUpdate()); // Returns the edited topic return editedTopic; } // Create and edit a topic async Task EditTopicExample() { // Creates and outputs a topic var createdTopic = await CreateTopic("Topic to Edit"); Debug.Log($"Created Topic ID: {createdTopic.Id}, Created Topic Title: {createdTopic.Title}"); // Edits and outputs the topic var editedTopic = await EditTopic(createdTopic, "New Title"); Debug.Log($"Edited Topic ID: {editedTopic.Id}, Edited Topic Title: {editedTopic.Title}"); } // Comment on a specific topic with a comment async Task<IComment> CreateComment(ITopic parentTopic, string comment) { // Creates a CommentCreation request object using a CommentCreationBuilder and populates it with the necessary fields var commentCreationBuilder = new CommentCreationBuilder(); commentCreationBuilder.SetText(comment); // Creates and retrieves the new comment var createdComment = await parentTopic.CreateCommentAsync(commentCreationBuilder.GetCommentCreation()); // Returns the new comment return createdComment; } // Edit a comment on a specific topic async Task<IComment> EditComment(ITopic parentTopic, IComment commentToEdit, string newComment) { // Creates a CommentUpdate request object using a CommentUpdateBuilder and populates it with the fields of the edited comment var commentUpdateBuilder = new CommentUpdateBuilder(commentToEdit); commentUpdateBuilder.SetText(newComment); // Updates and retrieves the comment var editedComment = await parentTopic.UpdateCommentAsync(commentUpdateBuilder.GetCommentUpdate()); // Returns the edited comment return editedComment; } // Create a topic and comment, then edit the comment async Task EditCommentExample() { // Creates and outputs a topic var parentTopic = await CreateTopic("Parent Topic"); Debug.Log($"Parent Topic ID: {parentTopic.Id}, Parent Topic Title: {parentTopic.Title}"); // Creates and outputs a comment on the parent topic var createdComment = await CreateComment(parentTopic, "A comment to edit"); Debug.Log($"Created Comment ID: {createdComment.Id}, Created Comment Title: {createdComment.Text}"); // Edits and outputs the comment var editedComment = await EditComment(parentTopic, createdComment, "A new comment"); Debug.Log($"Edited Comment ID: {editedComment.Id}, Edited Comment Title: {editedComment.Text}"); } }Note: You must fill in all the fields you want to keep in the edited topic. Any fields you don't fill in are replaced by empty fields in the original topic. Passing the topic to edit as a parameter to
TopicUpdateBuilderautomatically fills in all existing fields.- Check your console to confirm that you have successfully edited the topic.
