Skip to content

Commit

Permalink
added slightly hacky unload method for content
Browse files Browse the repository at this point in the history
  • Loading branch information
prime31 committed Feb 14, 2016
1 parent f39d0ab commit c1f581b
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Nez-PCL/Utils/NezContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;


namespace Nez.Systems
{
/// <summary>
/// ContentManager subclass that also manages Effects from ogl files. Adds asynchronous loading of assets as well.
/// </summary>
public class NezContentManager : Microsoft.Xna.Framework.Content.ContentManager
public class NezContentManager : ContentManager
{
Dictionary<string,Effect> _loadedEffects = new Dictionary<string,Effect>();

Expand Down Expand Up @@ -197,6 +198,42 @@ public void loadAsync<T>( string[] assetNames, Action onLoaded = null )
}


/// <summary>
/// removes assetName from LoadedAssets and Disposes of it. Note that this method uses reflection to get at the private ContentManager
/// disposeableAssets List.
/// </summary>
/// <param name="assetName">Asset name.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public void unloadAsset<T>( string assetName ) where T : class, IDisposable
{
if( isAssetLoaded( assetName ) )
{
try
{
var fieldInfo = typeof( ContentManager ).GetField( "disposableAssets", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic );
var assets = fieldInfo.GetValue( this ) as List<IDisposable>;

for( var i = 0; i < assets.Count; i++ )
{
var typedAsset = assets[i] as T;
if( typedAsset != null )
{
typedAsset.Dispose();
assets.RemoveAt( i );

LoadedAssets.Remove( assetName );
break;
}
}
}
catch( Exception e )
{
Debug.error( "Could not unload asset {0}. {1}", assetName, e );
}
}
}


/// <summary>
/// checks to see if an asset with assetName is loaded
/// </summary>
Expand Down

0 comments on commit c1f581b

Please sign in to comment.