Working with Assets in Unreal Engine

How to import various kind of assets to Unreal Engine with C++?

An Asset is any piece of content in an Unreal Engine project.

Check if a file exists on a disk

#include "HAL/PlatformFileManager.h" // Core
/**
 * Check whether a file exists on the disk
 * @param FilePath	The path of targeted file, e.g. "C:/Temp/MyFile.txt"
 * @param OutInfoMsg    Help for action's result
 * @return The string content of the file
*/
UFUNCTION(BlueprintCallable)
static bool DoesFileExist(FString FilePath, FString& OutInfoMsg);
bool UExampleClass::DoesFileExist(FString FilePath, FString &OutInfoMsg)
{
    bool bFileExist = FPlatformFileManager::Get().GetPlatformFile().FileExists(*FilePath);
    if(!bFileExist) OutInfoMsg = FString::Printf(TEXT("Error: No File found at: '%s'"), *FilePath);
    return bFileExist;
}

Open and Close Assets With C++ (Editor only)

Open an editor window for the desired Asset

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * Editor Only - Will not work in packaged build.
 * 
 * Open an editor window for the desired Asset
 *
 * @param Asset				Asset to open the editor window for
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 */
UFUNCTION(BlueprintCallable, Category = "Alex Quevillon - Open Asset")
static void OpenAssetWindow(UObject* Asset, bool& bOutSuccess, FString& OutInfoMsg);
#include "UnrealEdGlobals.h" // UnrealEd (Editor Only)
#include "Subsystems/AssetEditorSubsystem.h" // UnrealEd (Editor Only)

void UExampleClass::OpenAssetWindow(UObject* Asset, bool& bOutSuccess, FString& OutInfoMsg)
{
	// Validate Asset
	if (Asset == nullptr)
	{
		bOutSuccess = false;
		OutInfoMsg = FString::Printf(TEXT("Open Asset Window Failed - Asset is not valid."));
		return;
	}

	// Get Subsystem
	UAssetEditorSubsystem* AssetEditorSubsystem = GEditor ? GEditor->GetEditorSubsystem<UAssetEditorSubsystem>() : nullptr;
	if (AssetEditorSubsystem == nullptr)
	{
		bOutSuccess = false;
		OutInfoMsg = FString::Printf(TEXT("Open Asset Window Failed - Asset Editor Subsystem is not valid."));
		return;
	}

	// Open Window
	bOutSuccess = AssetEditorSubsystem->OpenEditorForAsset(Asset);
	OutInfoMsg = FString::Printf(TEXT("Open Asset Window %s"), *FString(bOutSuccess ? "Succeeded" : "Failed"));
}
if (Target.bBuildEditor == true)
{
   PrivateDependencyModuleNames.AddRange
   (
      new string[] 
      { 
         // Required Modules
         "UnrealEd",
      }
   );
}

Close the editor window that is open for the desired Asset

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * Editor Only - Will not work in packaged build.
 *
 * Close the editor window that is open for the desired Asset
 *
 * @param Asset				Asset to close the editor window for
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg	More information about the action's result
 */
UFUNCTION(BlueprintCallable, Category = "Alex Quevillon - Open Asset")
static void CloseAssetWindow(UObject* Asset, bool& bOutSuccess, FString& OutInfoMsg);
#include "UnrealEdGlobals.h" // UnrealEd (Editor Only)
#include "Subsystems/AssetEditorSubsystem.h" // UnrealEd (Editor Only)

void UExampleClass::CloseAssetWindow(UObject* Asset, bool& bOutSuccess, FString& OutInfoMsg)
{
	// Validate Asset
	if (Asset == nullptr)
	{
		bOutSuccess = false;
		OutInfoMsg = FString::Printf(TEXT("Close Asset Window Failed - Asset is not valid."));
		return;
	}

	// Get Subsystem
	UAssetEditorSubsystem* AssetEditorSubsystem = GEditor ? GEditor->GetEditorSubsystem<UAssetEditorSubsystem>() : nullptr;
	if (AssetEditorSubsystem == nullptr)
	{
		bOutSuccess = false;
		OutInfoMsg = FString::Printf(TEXT("Close Asset Window Failed - Asset Editor Subsystem is not valid."));
		return;
	}

	// Close Window
	int QuantityAssetClosed = AssetEditorSubsystem->CloseAllEditorsForAsset(Asset);

	bOutSuccess = QuantityAssetClosed > 0;
	OutInfoMsg = FString::Printf(TEXT("Close Asset Window %s"), *FString(bOutSuccess ? "Succeeded" : "Failed"));
}
if (Target.bBuildEditor == true)
{
   PrivateDependencyModuleNames.AddRange
   (
      new string[] 
      { 
         // Required Modules
         "UnrealEd",
      }
   );
}

Get all the assets that have an editor window open for them

#include "Kismet/BlueprintFunctionLibrary.h" // Engine
/**
 * Editor Only - Will not work in packaged build.
 *
 * Get all the assets that have an editor window open for them
 *
 * @param bOutSuccess		If the action was a success or not
 * @param OutInfoMsg		More information about the action's result
 * 
 * @return Assets that are open
 */
UFUNCTION(BlueprintCallable, Category = "Alex Quevillon - Open Asset")
static TArray<UObject*> GetAllAssetsWithOpenedWindow(bool& bOutSuccess, FString& OutInfoMsg);
#include "UnrealEdGlobals.h" // UnrealEd (Editor Only)
#include "Subsystems/AssetEditorSubsystem.h" // UnrealEd (Editor Only)
TArray<UObject*> UExampleClass::GetAllAssetsWithOpenedWindow(bool& bOutSuccess, FString& OutInfoMsg)
{
	// Get Subsystem
	UAssetEditorSubsystem* AssetEditorSubsystem = GEditor ? GEditor->GetEditorSubsystem<UAssetEditorSubsystem>() : nullptr;
	if (AssetEditorSubsystem == nullptr)
	{
		bOutSuccess = false;
		OutInfoMsg = FString::Printf(TEXT("Get All Assets With Opened Window Failed - Asset Editor Subsystem is not valid."));
		return TArray<UObject*>();
	}

	// Get Opened Assets
	TArray<UObject*> Assets = AssetEditorSubsystem->GetAllEditedAssets();

	bOutSuccess = true;
	OutInfoMsg = FString::Printf(TEXT("Get All Assets With Opened Window Succeeded"));
	return Assets;
}
if (Target.bBuildEditor == true)
{
   PrivateDependencyModuleNames.AddRange
   (
      new string[] 
      { 
         // Required Modules
         "UnrealEd",
      }
   );
}