UE5 C++ 读取图片插件(一)

原来UE可以使用 static,之前不知道,一用就报错。

复制代码
	static TSharedPtr<IImageWrapper> GetImageWrapperByExtention(const FString InImagePath);  //智能指针,方便追寻引用C++,加载ImageWrapper
	static UTexture2D* LoadTexture2D(const FString& ImagePath,bool &IsValid,int32& OutWidth,int32& OutHeight);	//加Const 为输入参数 ,不加为输出参数

	static void GetTextureFullNamesByPath(TArray<FString>&FileNames,const FString Path,EImageType ImageType);
	UFUNCTION(BlueprintCallable,Category = "GWX|TextureReader")
	static void GetTextureByPath(TArray<FTextureDetail>&Textures,FString FilePath,EImageType ImageType = EImageType::PNG);

GetTextureByPath 可以在关卡蓝图里被调用。但如果没有static,在关卡蓝图就调用不了。

C++ 插件开发

  • 创建函数库插件 配置模块创建变量

1.打开Plugins,点击左上角Add添加

2.这次我们选择创建蓝图库,方便蓝图中进行调取。插件启动时,注册运行这个模块。

3.ImageWrapper图像总的模块类。Core,UMG。

3.在TextureReaderBPLibrary.h里,添加需要使用的头文件。

cpp 复制代码
#include "IImageWrapper.h"      //图像信息的方法

#include "IImageWrapperModule.h"   //图片相关信息的模块

#include "Engine/Texture2D.h"

#include "CoreMinimal.h"   //

#include "Kismet/BlueprintFunctionLibrary.h"

4.接着创建两个枚举。用于图片类型,路径类型。UMETA 别名

cpp 复制代码
UENUM(BlueprintType)
enum class EImageType :uint8
{
	JPG UMETA(DisplayName = "JPG"),
	PNG UMETA(DisplayName = "PNG"),
	BMP UMETA(DisplayName = "BMP"),
	TIFF UMETA(DisplayName = "TIFF")
};

UENUM(BlueprintType)
enum class EPathType :uint8
{
	ABSOLUTE UMETA(DisplayName = "ABSOLUTE"),
	RELATIVE UMETA(DisplayName = "RELATIVE")
};

5.结构体用于区分,每个图片的具体信息。结构体名称不仅要F开头,而且要像个UE的类一样声明如下,可读可写。图片高和宽。

cpp 复制代码
USTRUCT(BlueprintType)  //F开头

struct FTextureDetail

{

GENERATED_USTRUCT_BODY()

UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "ZGD|TextureReader")

UTexture2D* Texture;

UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "ZGD|TextureReader")

int32 Width;

UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "ZGD|TextureReader")

int32 Height;

};

6.先声明需要这些,参数方法。在BlueprintLibrary中用static全局函数,这样在蓝图中可以直接去调用非常方便。智能指针,虚幻底层包含原生C++,智能指针可以包裹原生C++。在虚幻进行 引用计数 和 垃圾回收 很方便追寻,并且平台融合很好。

cpp 复制代码
UCLASS()

class UTetureReaderBPLibrary : public UBlueprintFunctionLibrary

{

GENERATED_UCLASS_BODY()



UFUNCTION(BlueprintCallable, meta = (DisplayName = "Execute Sample function", Keywords = "TetureReader sample test testing"), Category = "TetureReaderTesting")

static float TetureReaderSampleFunction(float Param);   //自带的静态

public:

static TSharedPtr<IImageWrapper> GetImageWrapperByExtention(const FString InImagePath);  //智能指针,方便追寻引用C++,加载ImageWrapper

static UTexture2D* LoadTexture2D(const FString& ImagePath, bool& IsValid, int32& OutWidth, int32& OutHeight); //加Const 为输入参数 ,不加为输出参数

};

7.图片加载,加载模块ImageWrapper(build.cs里添加的模块)。判断尾部,在返回对应的相关信息。

cpp 复制代码
TSharedPtr<IImageWrapper> UTetureReaderBPLibrary::GetImageWrapperByExtention(const FString InImagePath)
{
	IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));  //找到ImageWrapper这个模块
	if (InImagePath.EndsWith(".png"))
	{
		return ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
	}
	else if (InImagePath.EndsWith(".jpg")||InImagePath.EndsWith(".jpeg"))
	{
		return ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
	}
	else if (InImagePath.EndsWith(".bmp"))
	{
		return ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);
	}
	else if (InImagePath.EndsWith(".ico"))
	{
		return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICO);
	}
	else if (InImagePath.EndsWith(".exr"))
	{
		return ImageWrapperModule.CreateImageWrapper(EImageFormat::EXR);
	}
	else if (InImagePath.EndsWith(".icns"))
	{
		return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICNS);
	}
	else if (InImagePath.EndsWith(".tiff"))
	{
		return ImageWrapperModule.CreateImageWrapper(EImageFormat::TIFF);
	}
	return nullptr;
}

后续开始写第蓝图调用读取图片的函数。

相关推荐
豆沙沙包?2 分钟前
2025年--Lc182--sql(排序和分组)--Java版
java·数据库·sql
white-persist4 分钟前
JWT 漏洞全解析:从原理到实战
前端·网络·python·安全·web安全·网络安全·系统安全
IT_陈寒35 分钟前
React 性能优化:5个实战技巧让首屏加载提升50%,开发者亲测有效!
前端·人工智能·后端
CryptoRzz38 分钟前
欧美(美股、加拿大股票、墨西哥股票)股票数据接口文档
java·服务器·开发语言·数据库·区块链
rising start1 小时前
前端基础一、HTML5
前端·html·html5
杂货铺的小掌柜1 小时前
apache poi excel 字体数量限制
java·excel·poi
鬼谷中妖1 小时前
JavaScript 循环与对象:深入理解 for、for...in、for...of、不可枚举属性与可迭代对象
前端
大厂码农老A1 小时前
你打的日志,正在拖垮你的系统:从P4小白到P7专家都是怎么打日志的?
java·前端·后端
im_AMBER1 小时前
CSS 01【基础语法学习】
前端·css·笔记·学习
DokiDoki之父1 小时前
前端速通—CSS篇
前端·css