Delete topics and comments
You can use the Annotations package to delete existing topics and comments.
Delete an existing topic
To delete 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 DeleteTopicExample(); await DeleteCommentExample(); // 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; } // Create then delete a topic async Task DeleteTopicExample() { // Creates and outputs a topic var createdTopic = await CreateTopic("Topic to Delete"); Debug.Log($"Created Topic ID: {createdTopic.Id}, Created Topic Title: {createdTopic.Title}"); // Deletes the topic and outputs information await m_AnnotationRepository.DeleteTopicAsync(createdTopic.Id); Debug.Log($"Deleted Topic ID: {createdTopic.Id}"); } // 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; } // Create a topic with a comment, then delete the comment async Task DeleteCommentExample() { // 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 delete"); Debug.Log($"Created Comment ID: {createdComment.Id}, Created Comment Title: {createdComment.Text}"); // Deletes the comment and outputs information await parentTopic.DeleteCommentAsync(createdComment.Id); Debug.Log($"Deleted Comment ID: {createdComment.Id}"); } }Check your console to confirm that you have successfully deleted the topic.
