9#include "Editor/UnrealEdEngine.h"
11#include "AssetToolsModule.h"
12#include "ContentBrowserModule.h"
13#include "IContentBrowserSingleton.h"
14#include "ObjectTools.h"
15#include "UObject/Package.h"
16#include "TextureResource.h"
17#include "Engine/Texture2D.h"
18#include "AssetRegistry/AssetRegistryModule.h"
19#include "AssetRegistry/AssetData.h"
20#include "Modules/ModuleManager.h"
21#include "UObject/SavePackage.h"
22#include "HAL/UnrealMemory.h"
29 TArray<FAssetData> SelectedAssets;
30 FContentBrowserModule& ContentBrowserModule = FModuleManager::LoadModuleChecked<FContentBrowserModule>(TEXT(
"ContentBrowser"));
31 IContentBrowserSingleton& ContentBrowserSingleton = ContentBrowserModule.Get();
32 ContentBrowserSingleton.Get().GetSelectedAssets(SelectedAssets);
34 for (
const FAssetData& Asset : SelectedAssets)
39 UE_LOG(LogTemp, Warning, TEXT(
"EditorUtilities.cpp: Export complete."));
46 int32 pathSeparatorIdx;
47 FAssetData obj = Asset;
48 FString GamePath = obj.GetAsset()->GetPathName();
51 if (GamePath.FindLastChar(
'/', pathEnd))
54 AssetName += GamePath;
55 AssetName.RightChopInline(pathEnd);
57 if (AssetName.FindChar(
'.', extensionIdx))
59 AssetName.LeftInline(extensionIdx);
60 GamePath.LeftInline(pathEnd);
61 FString Suffix =
"T_";
62 FString NameWithSuffix = Suffix + AssetName;
63 AssetName = NameWithSuffix;
67 AssetName =
"T_Thumbnail";
70 if (AssetName.FindChar(
'/', pathSeparatorIdx))
76 FObjectThumbnail* thumb = ThumbnailTools::GenerateThumbnailForObjectToSaveToDisk(obj.GetAsset());
82 FString PackageName = TEXT(
"/Game/Agrarsense/Misc/ExportedThumbnailTextures/");
83 if (!PackageName.EndsWith(
"/"))
87 PackageName += AssetName;
89 UPackage* Package = CreatePackage(*PackageName);
92 UTexture2D* NewTexture = NewObject<UTexture2D>(Package, *AssetName, RF_Public | RF_Standalone | RF_MarkAsRootSet);
93 NewTexture->AddToRoot();
94 FTexturePlatformData* platformData =
new FTexturePlatformData();
95 platformData->SizeX = thumb->GetImageWidth();
96 platformData->SizeY = thumb->GetImageHeight();
98 platformData->PixelFormat = EPixelFormat::PF_B8G8R8A8;
99 NewTexture->SetPlatformData(platformData);
101 FTexture2DMipMap* Mip =
new FTexture2DMipMap();
102 platformData->Mips.Add(Mip);
103 Mip->SizeX = thumb->GetImageWidth();
104 Mip->SizeY = thumb->GetImageHeight();
106 Mip->BulkData.Lock(LOCK_READ_WRITE);
107 uint8* TextureData = (uint8*)Mip->BulkData.Realloc(thumb->GetUncompressedImageData().Num() * 4);
108 FMemory::Memcpy(TextureData, thumb->GetUncompressedImageData().GetData(), thumb->GetUncompressedImageData().Num());
109 Mip->BulkData.Unlock();
111 NewTexture->Source.Init(thumb->GetImageWidth(), thumb->GetImageHeight(), 1, 1, ETextureSourceFormat::TSF_BGRA8, thumb->GetUncompressedImageData().GetData());
112 NewTexture->LODGroup = TEXTUREGROUP_UI;
113 NewTexture->UpdateResource();
114 Package->MarkPackageDirty();
115 Package->FullyLoad();
116 FAssetRegistryModule::AssetCreated(NewTexture);
118 FSavePackageArgs SaveArgs;
119 SaveArgs.TopLevelFlags = EObjectFlags::RF_Public | EObjectFlags::RF_Standalone;
120 SaveArgs.SaveFlags = SAVE_NoError;
121 SaveArgs.bForceByteSwapping =
true;
122 FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
123 UPackage::SavePackage(Package, NewTexture, *PackageFileName, SaveArgs);
static void CreateThumbnailTextureFromAsset(const FAssetData &Asset)
static void ExportThumbnailTextureFromSelectedAssets()