Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call Resources.UnloadUnusedAssets() more than once every 3 mins #1787

Merged
merged 2 commits into from Apr 20, 2020

Conversation

petchema
Copy link
Collaborator

@petchema petchema commented Apr 18, 2020

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.

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 Interkarma#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, for optimal efficiency, all code using
Resources.UnloadUnusedAssets() (say mods) should call either
ThrottledUnloadUnusedAssets() or ForcedUnloadUnusedAssets() instead to
prevent unnecessary collections.
Comment on lines 24 to 27
private DaggerfallGC()
{
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason you used a private constructor that never get called instead of a static class or you just preferred this way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, just Java habits!

@Interkarma
Copy link
Owner

Thank you, happy for this workaround. Even just minimising the problem goes a long way to making the experience better. Cheers! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants