From ef00d30f0414bdf427de5d8ca38e55a8d86a353b Mon Sep 17 00:00:00 2001 From: Mike Desaro Date: Sun, 9 Oct 2016 17:26:17 -0700 Subject: [PATCH] fixed bug with unloading assets --- Nez-PCL/Utils/NezContentManager.cs | 33 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Nez-PCL/Utils/NezContentManager.cs b/Nez-PCL/Utils/NezContentManager.cs index 0a157ba7e..4006623fc 100644 --- a/Nez-PCL/Utils/NezContentManager.cs +++ b/Nez-PCL/Utils/NezContentManager.cs @@ -207,26 +207,39 @@ public void unloadAsset( string assetName ) where T : class, IDisposable { try { - var fieldInfo = typeof( ContentManager ).GetRuntimeField( "disposableAssets" ); - var assets = fieldInfo.GetValue( this ) as List; + FieldInfo fieldInfo = null; + var fields = typeof( ContentManager ).GetRuntimeFields(); + foreach( var field in fields ) + { + if( field.Name == "disposableAssets" ) + { + fieldInfo = field; + break; + } + } + // first fetch the actual asset. we already know its loaded so we'll grab it directly + #if FNA + fieldInfo = typeof( ContentManager ).GetRuntimeField( "loadedAssets" ); + var LoadedAssets = fieldInfo.GetValue( this ) as Dictionary; + #endif + + var assetToRemove = LoadedAssets[assetName]; + + var assets = fieldInfo.GetValue( this ) as List; for( var i = 0; i < assets.Count; i++ ) { + // see if the asset is disposeable. If so, find and dispose of it. var typedAsset = assets[i] as T; - if( typedAsset != null ) + if( typedAsset != null && typedAsset == assetToRemove ) { typedAsset.Dispose(); assets.RemoveAt( i ); - - #if FNA - fieldInfo = typeof( ContentManager ).GetRuntimeField( "loadedAssets" ); - var LoadedAssets = fieldInfo.GetValue( this ) as Dictionary; - #endif - - LoadedAssets.Remove( assetName ); break; } } + + LoadedAssets.Remove( assetName ); } catch( Exception e ) {