feat: backport API to Unity 2021 for compatibility #337
+205
−52
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Library requires Unity 2022.2+ due to use of three new APIs (Application.exitCancellationToken, MonoBehaviour.destroyCancellationToken, Editor.DiscardChanges).
Users on older Unity versions cannot use the library without modifications.
This PR adds backward compatibility for Unity 2021+ by providing polyfill implementations for three APIs, using conditional compilation to select the appropriate implementation based on the Unity version:
ApplicationExitCompat.csto provide a fallbackexitCancellationTokenfor Unity versions older than 2022.2. On Unity 2022.2 or newer, the officialApplication.exitCancellationTokenis used.YarnProjectImporterEditor.csto callEditor.DiscardChanges()on Unity 2022.2 or newer, and fall back to the olderResetValues()method on earlier editor versions.MonoBehaviourExtensions.GetDestroyCancellationToken(this MonoBehaviour)as a compatibility extension: On Unity 2022.2 or newer it returnsmb.destroyCancellationToken; on older versions it attaches (or reuses) an internalDestroyTokenNotifiercomponent to hold aCancellationTokenSourcethat is cancelled inOnDestroy(), and returns that token.This allows the library to be used on older Unity versions without requiring an upgrade, while preserving the behavior on newer Unity versions.
Nope. This change is fully backward-compatible and does not alter behavior for users on Unity 2022+.
This PR allows users on Unity 2021 to use the latest Yarn Spinner 3.0, which I think is a good thing.
I did my best to provide polyfills for the three new APIs, but I’m not claiming it’s perfect.
I’d really appreciate any feedback—if anything could be improved or done differently, feel free to suggest or adjust.
The goal is just to help more users enjoy the library across different Unity versions.