.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintAsyncActionBase.h"
#include "AsyncImportFileAsTexture2D.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnImportImage, UTexture2D*, InTargetImage);
/**
* 异步加载图片
*/
UCLASS()
class EDUMG_API UAsyncImportFileAsTexture2D : public UBlueprintAsyncActionBase
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true", Category = "Tool|AsyncImportFileAsTexture2D", WorldContext = "WorldContextObject"))
static UAsyncImportFileAsTexture2D* AsyncImportFileAsTexture2D(UObject* WorldContextObject, const FString& InTargetFilePath);
/** Delegate called when the save/load completes */
UPROPERTY(BlueprintAssignable)
FOnImportImage Completed;
/** Execute the actual operation */
virtual void Activate() override;
protected:
// 目标文件路径
FString TargetFilePath;
};
.cpp
#include "FunctionLibrary/AsyncImportFileAsTexture2D.h"
#include "ImageUtils.h"
UAsyncImportFileAsTexture2D* UAsyncImportFileAsTexture2D::AsyncImportFileAsTexture2D(UObject* WorldContextObject, const FString& InTargetFilePath)
{
UAsyncImportFileAsTexture2D* Action = NewObject<UAsyncImportFileAsTexture2D>();
Action->TargetFilePath = InTargetFilePath;
Action->RegisterWithGameInstance(WorldContextObject);
return Action;
}
void UAsyncImportFileAsTexture2D::Activate()
{
AsyncTask(ENamedThreads::GameThread, [&]()
{
UTexture2D* Image = FImageUtils::ImportFileAsTexture2D(TargetFilePath);
if (this->Completed.IsBound())
{
this->Completed.Broadcast(Image);
}
SetReadyToDestroy();
});
}
UE4_蓝图异步节点_异步加载图片
最新推荐文章于 2023-03-20 10:44:13 发布