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

Added selective ContentManager asset unloading #6663

Merged
merged 1 commit into from Apr 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions MonoGame.Framework/Content/ContentManager.cs
Expand Up @@ -471,6 +471,58 @@ public virtual void Unload()
loadedAssets.Clear();
}

/// <summary>
/// Unloads a single asset.
/// </summary>
/// <param name="assetName">The name of the asset to unload. This cannot be null.</param>
public virtual void UnloadAsset(string assetName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does overloading Unload make more sense than UnloadAsset, since the load method is Load, not LoadAsset?

{
if (string.IsNullOrEmpty(assetName))
{
throw new ArgumentNullException("assetName");
}
if (disposed)
{
throw new ObjectDisposedException("ContentManager");
}

//Check if the asset exists
object asset;
if (loadedAssets.TryGetValue(assetName, out asset))
{
//Check if it's disposable and remove it from the disposable list if so
var disposable = asset as IDisposable;
if (disposable != null)
{
disposable.Dispose();
disposableAssets.Remove(disposable);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why disposableAssets is a list, since it seems like it's only used as a set. Should it be changed to a set for perf here?

}

loadedAssets.Remove(assetName);
}
}

/// <summary>
/// Unloads a set of assets.
/// </summary>
/// <param name="assetNames">The names of the assets to unload.</param>
public virtual void UnloadAssets(IList<string> assetNames)
Copy link
Contributor

Choose a reason for hiding this comment

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

For usability, can this be params string[] assetNames instead?

{
if (assetNames == null)
{
throw new ArgumentNullException("assetNames");
}
if (disposed)
{
throw new ObjectDisposedException("ContentManager");
}

for (int i = 0; i < assetNames.Count; i++)
{
UnloadAsset(assetNames[i]);
}
}

public string RootDirectory
{
get
Expand Down