Is it possible to force-unload an asset in editor?

I have an issue with some widgets initializing while some of their textures are not loaded yet. This causes widgets to work on incorrect texture size data, and as a result, after images load, they appear stretched (I know about ScaleBox, but it doesn’t help if the image inside it has a brush of an incorrect size).

I have ideas on how to fix the issue, but the problem is, it’s not exactly convenient to do, because this problem reproduces only once per editor session, since after a texture loads once - it doesn’t unload anymore.

Is there anything I can do to avoid having to restart the editor to unload resources?

Hello,

Yes, you can force-unload assets in Unreal Engine to help with debugging issues like the one you described. This can be done using the FlushAsyncLoading function and manually unloading assets. Here are some steps and methods you can use to achieve this:

Flush Async Loading:
To ensure that all pending async loads are completed before you attempt to unload assets, use the FlushAsyncLoading function. HealthCareGov

FlushAsyncLoading();

Manually Unload Assets:
Use the UnloadUnusedAssets function to unload assets that are not currently in use.

GEngine->ForceGarbageCollection(true);

This will perform a garbage collection pass, which will unload any assets that are no longer referenced in memory.

Use the Content Browser:
If you prefer a non-code approach, you can unload specific assets using the Content Browser in the Unreal Editor:

Right-click on the asset you want to unload.
Select “Asset Actions”.
Click on “Unload Asset”.
Editor Utility Widgets or Blueprints:
Create an Editor Utility Widget or Blueprint to automate the process of unloading assets. This can be particularly useful for repetitive tasks.

Create an Editor Utility Widget or Blueprint.
Use the Unload Asset node to specify the asset you want to unload.
Console Command:
You can also use the console command to perform garbage collection, which might help in unloading unused textures and other assets.

gc.CollectGarbage

This command forces a garbage collection pass, similar to using ForceGarbageCollection in code.

Here’s a sample implementation in C++:

#include "Engine/Engine.h"
#include "Engine/AssetManager.h"

void MyUnloadAssetsFunction()
{
    // Ensure all async loads are completed
    FlushAsyncLoading();
    
    // Force garbage collection
    GEngine->ForceGarbageCollection(true);
    
    // Unload specific assets if needed
    // UAssetManager::Get().UnloadAssetData(UYourAssetType::StaticClass());
}

By using these methods, you can avoid having to restart the editor to unload resources, and better manage the texture loading and initialization of your widgets.

1 Like