Unload Editor Assets

Unreal uses the RF_Standalone flag to ensure assets that are loaded in the editor stay loaded, even if garbage collection occurs and the assets aren't referenced by anything. This is ideal behavior in most cases as it would be a nuisance to constantly re-load assets when working in the editor.

However, in some cases (such as debugging async load behavior or engine hitching) it is desirable to unload editor assets without having to restart the editor. To do this, we have to remove the RF_Standalone flag from a loaded asset and force garbage collection. A quick way to do this is to iterate over all loaded objects and clear the flag:

void UGameUtilities::UnloadAllAssets(UObject* WorldContextObject)
{
    if (!WorldContextObject) return;

    for (TObjectIterator<UObject> It; It; ++It)
    {
       UObject* LoadedObject = *It;
       if (LoadedObject && LoadedObject->HasAnyFlags(RF_Standalone))
       {
          LoadedObject->ClearFlags(RF_Standalone);
       }
    }

    if (GEngine)
    {
       GEngine->ForceGarbageCollection(true);
    }
}

Note: The editor will prevent you from saving assets that have the RF_Standalone flag removed in order to prevent data loss. You can get around this by adding the following argument to the build configuration in your IDE:

-dpcvars=save.FixupStandaloneFlags=1

This will ensure the RF_Standalone flag is re-added when the asset is saved.

Last updated