Is it possible to “unload” objects immediately that are loaded from editor scripts via AssetDatabase.LoadAssetAtPath? I’m using Unity 3.5.
I’m working on a game with a lot of asset bundles with a lot of data in them. I have a script that basically does this:
// load data file with description of what bundles to make
foreach( var bundleDesc in bundlesToMake )
{
var objectsToBundle = new List<Object>();
foreach( var obj in bundleDesc.ObjPathsToLoad )
{
objectsToBundle.Add( AssetDatabase.LoadAssetAtPath( obj ) );
}
BuildPipeline.BuildAssetBundle( obj.pathToBundleTo, objectsToBundle.ToArray() );
}
The problem I’m running into is that if I want to do this for all of my bundles I’ll run out of memory in the editor. Each asset bundle it tries to create increases Unity’s “Real Memory” as reported by Activity Monitor to the point where it’ll eventually throw an error after it reaches 3.5 gigs or so (which makes sense, given that Unity is a 32 bit application).
I’ve tried the following:
Iterating over all the objects and calling DestroyImmediate( obj, false )
Calling Resources.UnloadAsset( obj ) on all the objects after they’re bundled
Calling Resources.UnloadUnusedAssets after each bundle is built
None of which seem to reduce the amount of memory being used from bundle to bundle.
Is there something I’m missing, or a different approach I could take (short of just not building all the assets at once) to help alleviate my memory issues?
I don’t know if it will help you, but I have had luck using System.GC.Collect() after EditorUtility.UnloadUnusedAssets(). I don’t know which one does the trick, you can figure that out if you want, or just use both.
Resources.Unload, etc are meant for runtime I believe. Just make sure you null out any references you don’t need anymore before calling these.
We’re having the same problem, and none of the proposed solutions seem to work.
After each AssetBundle that we create, we call EditorUtility.UnloadUnusedAssets(). If you keep your eyes on the memory use of the process, you can see the clear spikes up and drops down where this is occurring, so initially it seems to solve the issue. But as the build process continues, the overall memory being used by Unity continues to grow gradually. Eventually, despite unloading unused assets after each AB, the editor approaches 3.5 GB and crashes. Seems to me there’s a memory leak in Unity.
I added both GC.Collect() and EditorApplication.NewScene() after EditorUtility.UnloadUnusedAssets() and the crash still occurred. It could be these actions help slightly, just not enough (provided the build goes on long enough, for us it’s about 45 minutes before crashing).
that sounds somewhat related to a problem that we had recently.
We had a build script that generated a whole bunch of prefabs out of various scenes. During this process the editor memory consumption was really high (~3GB). We couldn’t even perform the build on a Mac machine, as it would always run out of memory.
Our solution was simply to call AssetDatabase.SaveAssets() after each single prefab has been generated.
MPanknin : the issue is about building assetbundle, not about generating prefab.
Any solution on this problem?
I have exactly same problem especially on mac with Unity 5.5.0p1. BuildPipeline.BuildAssetBundle has been obsoluted, but I still want to use it, because BuildAssetBundles are much more slower than BuildAssetBundle.