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
for managing tasks that should be cancelled when the GameObject
is disabled or destroyed. This is particularly useful in scenarios where you have long-running tasks or operations that should not continue if the GameObject
is no longer available.
Example
// Example of using EnabledToken to cancel a task when the GameObject is disabled or destroyed
public async Task PerformOperationAsync(GameObject gameObject)
{
try
{
// Use the EnabledToken to cancel the operation if the GameObject is disabled or destroyed
await SomeLongRunningOperationAsync(gameObject.EnabledToken);
}
catch (OperationCanceledException)
{
// Handle the cancellation
Console.WriteLine("Operation was cancelled because the GameObject was disabled or destroyed.");
}
}