Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Release()

Releases the loaded object so that Unity can unload it.
Read time 1 minuteLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: Unity.Loading
  • Assembly: UnityEngine.ContentLoadModule
public void Release()

Remarks

Unity reference counts the loaded object, so loading the same asset through several Loadable<T> instances works correctly, and the object unloads only when the last reference is released.
Each Loadable<T> instance holds at most one reference. Repeated Loadable<T>.Load or Loadable<T>.LoadAsync calls on the same instance return the same load operation without taking another reference, so a single Release frees it and any further Release does nothing. Release every instance that you loaded, otherwise the object stays loaded after your code stops using it.
Release is not recursive. If this Loadable references a ScriptableObject that has loaded Loadable<T> fields of its own, releasing this Loadable does not release those inner ones. Release each Loadable<T> that was loaded.
The
OnDestroy
message of a MonoBehaviour is a good place to release its Loadable<T> fields, so that unloading a scene or destroying a prefab instance does not leave content loaded that nothing can reach.
Release has no effect on content that was loaded from the AssetDatabase in Play mode, because that content is not reference counted. Refer to the Loadable<T> remarks for details.