Do not call Resources.UnloadUnusedAssets() more than once every 3 mins #1787
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.
Daggerfall Unity needs to explicitly call Resources.UnloadUnusedAssets() because it barely uses SceneManager.LoadScene() which is the way games usually indirectly call it.
This is currently done in two places, including while closing UI windows, a path taken often enough during normal gameplay to prevent unused assets to pile up too much.
However this can be a lengthy operation that blocks the whole environment as it calls GC.Collect() internally, and can make sound skip which can be pretty annoying once you noticed it (issue #1539).
This patch makes sure the call to UnloadUnusedAssets() does not happen more than once every 3 minutes, a tradeoff sufficient to make that issue much less noticeable, yet should prevent unused assets to stay around forever.
It should be noted that, while not required, for optimal efficiency all code using Resources.UnloadUnusedAssets() (say mods) should call either ThrottledUnloadUnusedAssets() or ForcedUnloadUnusedAssets() instead to prevent unnecessary collections.