UE5-创建蓝图插件

编辑下点击插件,点击添加,选择蓝图库,输入插件名称,点击创建。

过一会自动打开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创建出来;

运行如下,中心图片为通过蓝图加载的指定路径下的图片。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖儿糖儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值