编辑下点击插件,点击添加,选择蓝图库,输入插件名称,点击创建。
过一会自动打开VS
找到(插件名称)BPLibrary.h和.cpp
本文以根据路径读取图片信息为例子
.h 文件如下:
#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "ImageLoadHelpBPLibrary.generated.h"
UCLASS()
class UImageLoadHelpBPLibrary : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
//通过路径获取单张图片,转为Texture2D
UFUNCTION(BlueprintCallable, Category = "Image")
static UTexture2D* LoadTexture2D(const FString ImagePath);
//获取指定路径下的所以有图片的名称
UFUNCTION(BlueprintCallable, Category = "Image")
static TArray<FString> GetFolderFiles(FString ImagePath);
//将指定路径下的所有图片转为Texture2D
UFUNCTION(BlueprintCallable, Category = "Image")
static TArray<UTexture2D*> GetAllImageFromFiles(FString ImagePath);
//判断图片类型
static TSharedPtr<class IImageWrapper>GetImageWrapperByExtention(const FString ImagePath);
};
UFUNCTION(BlueprintCallable, Category = "Image")
其中UFUNCTION 为蓝图函数的标识,Category = "Image"为节点
static UTexture2D* LoadTexture2D(const FString ImagePath);
通过搜索LoadTexture2D即可找到该蓝图函数。
.cpp文件如下:
// Copyright Epic Games, Inc. All Rights Reserved.
#include "ImageLoadHelpBPLibrary.h"
#include "ImageLoadHelp.h"
#include <IImageWrapper.h>
#include <IImageWrapperModule.h>
UImageLoadHelpBPLibrary::UImageLoadHelpBPLibrary(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
UTexture2D* UImageLoadHelpBPLibrary::LoadTexture2D(FString ImagePath)
{
UTexture2D* Texture = nullptr;
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*ImagePath))
{
return nullptr;
}
TArray<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *ImagePath))
{
return nullptr;
}
TSharedPtr ImageWrapper = GetImageWrapperByExtention(ImagePath);
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
TArray<uint8> UncompressedRGBBA;
if (ImageWrapper->GetRaw(ERGBFormat::RGBA, 8, UncompressedRGBBA))
{
Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_R8G8B8A8);
if (Texture != nullptr)
{
void* TextureData = Texture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedRGBBA.GetData(), UncompressedRGBBA.Num());
Texture->GetPlatformData()->Mips[0].BulkData.Unlock();
Texture->UpdateResource();
}
}
}
return Texture;
}
TArray<FString> UImageLoadHelpBPLibrary::GetFolderFiles(FString ImagePath)
{
TArray<FString>files;
FPaths::NormalizeDirectoryName(ImagePath);
IFileManager& FileManager = IFileManager::Get();
FString FinalPath = ImagePath / TEXT("*");
FileManager.FindFiles(files, *FinalPath, true, true);
return files;
}
TArray<UTexture2D*> UImageLoadHelpBPLibrary::GetAllImageFromFiles(FString ImagePath)
{
TArray<FString> ImgPath = GetFolderFiles(ImagePath);
TArray<UTexture2D*>Texture2DArr;
for (auto path : ImgPath)
{
UTexture2D* Texture2D = LoadTexture2D(ImagePath + "/" + path);
Texture2DArr.Add(Texture2D);
}
return Texture2DArr;
}
TSharedPtr<class IImageWrapper> UImageLoadHelpBPLibrary::GetImageWrapperByExtention(const FString ImagePath)
{
IImageWrapperModule& module = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
if (ImagePath.EndsWith(".png"))
{
return module.CreateImageWrapper(EImageFormat::PNG);
}
if (ImagePath.EndsWith(".jpg") || ImagePath.EndsWith("jpeg"))
{
return module.CreateImageWrapper(EImageFormat::JPEG);
}
return nullptr;
}
返回UE,新建一个UMG,添加一个image控件,并在细节栏勾选为变量;
编写蓝图;
在关卡蓝图中将UMG创建出来;
运行如下,中心图片为通过蓝图加载的指定路径下的图片。