CancellationToken EnabledToken { get; set; }

book_4_sparkGenerated
code_blocksInput

Description

This property provides a CancellationToken that is cancelled when the GameObject ceases to exist or is disabled. This can be useful for managing asynchronous operations that should be terminated when the GameObject is no longer active or valid.

Usage

Use the EnabledToken property to obtain a CancellationToken that you can pass to asynchronous methods or tasks. This token will automatically be cancelled if the GameObject is destroyed or disabled, allowing you to gracefully handle the termination of ongoing operations.

Example

// Example of using EnabledToken in an asynchronous method
public async Task PerformOperationAsync(GameObject gameObject)
{
    try
    {
        // Pass the EnabledToken to an asynchronous method
        await SomeAsyncMethod(gameObject.EnabledToken);
    }
    catch (OperationCanceledException)
    {
        // Handle the cancellation
        Console.WriteLine("Operation was cancelled because the GameObject was disabled or destroyed.");
    }
}