Editor Running Out Of Memory

Hi ,

I have the following Editor Wizard script, it goes through a list of GameObjects and generates asset bundles for each. Unfortunately Unity is running out of memory and crashing. Every time Unity adds a GameObject the memory shoots up and will not be freed. I have tried using Resources.Unload… but it does not seem to be working. How can i free memory in the editor?

Thanks

Karl

    void OnWizardCreate()
    {
        for( int i = 0; i < settings.objectsToProcess.Length; ++i )
        {
            // Get original path and out path        
            string originalDir = Path.GetDirectoryName( AssetDatabase.GetAssetPath( settings.objectsToProcess[i] ) );
            string outPath = originalDir.Replace( settings.inputPathRoot, settings.outputPathRoot );
            Directory.CreateDirectory( outPath );

            if( EditorUtility.DisplayCancelableProgressBar( string.Format( "Generating Asset Bundles {0}/{1}", i, settings.objectsToProcess.Length ), "Creating AssetBundles", ( float )( i / settings.objectsToProcess.Length ) ) )
            {
                return;
            }

            GenerateAssetBundles( settings.objectsToProcess[i], outPath );
        }
        
        EditorUtility.ClearProgressBar();
    }
   void GenerateAssetBundles( GameObject original, string saveToDir )
    {
        // Create an instance
        GameObject go = Instantiate( original ) as GameObject;
        go.name = original.name;

        // Clean up object. Remove components we dont need.
        RemoveComponents( typeof( LODGroup ), go );
        RemoveComponents( typeof( Animation ), go );
        
        // Assign material shader to use
        go.GetComponentsInChildren<Renderer>().ToList().
            ForEach( r => r.sharedMaterials.ToList().
                ForEach( m => m.shader = settings.shaderToUse ) );

        // Orphan the objects
        List<GameObject> objs = go.GetComponentsInChildren<Transform>().Select( t => t.gameObject ).ToList();
        objs.ForEach( obj => obj.transform.parent = null );

        // Check texture sizes
        objs.ForEach( obj => SetTextureSizes( obj ) );

        // Save each object as an asset bundle
        foreach( GameObject outObj in objs )
        {
            // Can not export scene objects, must first place them into a prefab
            string filePath = saveToDir + "/" + outObj.name;            
            GameObject pr = PrefabUtility.CreatePrefab( filePath + ".prefab", outObj );
            BuildPipeline.BuildAssetBundle( pr, new Object[1] { pr }, filePath + ".unity3d", BuildAssetBundleOptions.CollectDependencies, settings.buildTarget, settings.buildOptions );

            // Cleanup, delete the prefab and object
            File.Delete( filePath + ".prefab" );
            DestroyImmediate( outObj );
            DestroyImmediate( pr );
            Resources.UnloadUnusedAssets();
        }        
    }

I have noticed that as soon as i call .sharedMaterials the memory is shooting up.

E.G

    void GenerateAssetBundles( GameObject original, string saveToDir )
    {
        // Create an instance
        GameObject go = Instantiate( original ) as GameObject;
        go.name = original.name;

        // Clean up object. Remove components we dont need.
        RemoveComponents( typeof( LODGroup ), go );
        RemoveComponents( typeof( Animation ), go );
        
        // Assign material shader to use
        //go.GetComponentsInChildren<Renderer>().ToList().
        //    ForEach( r => r.sharedMaterials.ToList().
        //        ForEach( m => m.shader = settings.shaderToUse ) );

        foreach( Renderer r in go.GetComponentsInChildren<Renderer>() )
        {            
            foreach( Material m in r.sharedMaterials )
            {
                m.shader = settings.shaderToUse;                
            }
        }
     }