Resources.UnloadAsset()

This function is mentioned in the release notes for 3.5.2, but as yet is not in the online documentation. (Now there is a description in the documentation in the download, but it’s not helping me).

I’m getting the message “UnloadAsset may only be used on individual assets and can not be used on GameObject’s / Components or AssetBundles” when I call it with a prefab that was returned by Resources.Load(). The prefab is a GameObject though, so I guess that’s not going to work.

Here’s the current on disk documentation:

static function UnloadAsset (assetToUnload : Object) : void

Description

Unloads assetToUnload from memory.

This function can only be called on Assets that are stored on disk.

If there are any references from game objects in the scene to the asset and it is being used then Unity will reload the asset from disk as soon as it is accessed.

The function takes Object as an argument, so I’m assuming it’s not wanting the String I originally called Load() with.

How is UnloadAsset() meant to be used?

It looks like you just feed the GameObject as the overload. You stated “it’s not wanting the String” which is true, it needs the object to unload. Unlike Load() which requires a String as path, this requires the actual Object in question. If you have the object in memory(stored in a var or array) then you can remove it completely from the memory.

var myObject:Object;

function Start()
{
    myObject=Resources.Load("PathToObject");
}

// When you want to unload that object

Resources.UnloadAsset(myObject);

If you are unfamiliar with Object, it is the base type of all objects used in Unity (Texture,GameObject,Transform,Texture2D…)

I’ve just tested on PC standalone and looks that it works following:

  1. Destroy Object containing references to asset1
  2. call Resources.UnloadAsset( asset1 );
  3. call Resources.UnloadUnusedAssets() immediately after step 2;

If you don’t do step 3 - app crashed. Also looks that step 2 only marks object for deletion, because actual work done in step 3( i’ve measured time of coroutine with Resources.UnloadUnusedAssets() inside ).

Also Unity Editor warns(as you described) when you trying to Unload resource that used at current scene.
So you need to destroy it at first.

Unity 4.2.2f1

I had to use Resources.UnloadAsset to take textures out of memory in a long looping ImageSequence (I found that on the Android build, but not the iOS strangely, the phone would crash after hitting a certain number of frames).

So I created a holding object whose only purpose was to then be unloaded in order to get the texture out of the memory. So in the ImageSequenceSingleTexture script from DimasTheDriver at 41 Post I added a second texture (“private Texture lastTexture;”), in the PlayLoop, I load this object with the texture file I want to unload, and then in the “void Update” I call, “Resources.UnloadAsset(this.lastTexture);”

It seems to solve the crashing problem without creating any jitter in the framerate.