Brief new API feedback

This is a really quick thread to summarize my thoughts after looking through the changelogs and C# reference source changes related to the newly-released 2022.2 alpha.

CancellationToken properties
This is a really nice change, I’ve wanted something like Application.CancellationToken for quite some time now. I am however concerned about the choice to expose a cancellation token on every single MonoBehaviour. It appears to be lazily-allocated, but that’s still a pointer of memory overhead for every single MonoBehaviour instance. Maybe instead it might be better to track these outside of the MonoBehaviour instance and offer a GetOrCreateCancellationToken-like API?

Access to Mesh.bindposes as NativeArray
This is also really nice, and will allow me to reduce some allocations. However, Mesh.bindposeCount doesn’t appear to be settable, meaning I still need to allocate a dummy managed array to resize the buffer.

I’d also like to bring up my earlier 2022.1 threading wishlist post, as there still doesn’t appear to be a proper API to retrieve the main thread’s SynchronizationContext. I’ve updated that thread to contain a more appropriate API proposal, as the Object.IsAlive method previously suggested there was shown to not be necessary.

9 Likes

Undo System: Added: UndoRedoEvent callback containing undo information added.
is also a welcome addition when working on editor tools.

On CancellationToken, i think the naming is quite confusing. It tells me the type of the property, but not what it does.

IMO it should convey that it cancels when the object is destroyed, as opposed to Disabled for example. Something like MonoBehaviour.DestroyedCancellationToken?

4 Likes

That’s really useful, I currently use Unitask’s hack for a destruction token.
Is there also a OnDisabled token? That one would be even more valuable than the OnDestroy token for most of my purposes. (assuming I don’t want to destroy/recreate modeless UI objects all the time, but rather enable them when needed)

2 Likes