Description
The EnabledToken
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.
Usage
Use the EnabledToken
to monitor the lifecycle of a GameObject
. This token can be passed to asynchronous methods to ensure they are cancelled if the GameObject
is disabled or destroyed.
Example
// Example of using EnabledToken with an asynchronous operation
public async Task PerformOperationAsync(GameObject gameObject)
{
try
{
// Pass the EnabledToken to the asynchronous method
await SomeAsyncMethod(gameObject.EnabledToken);
}
catch (OperationCanceledException)
{
// Handle the cancellation
Console.WriteLine("Operation was cancelled because the GameObject was disabled or destroyed.");
}
}