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;
}

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

相关推荐
小声读源码3 分钟前
【技巧】dify前端源代码修改第一弹-增加tab页
前端·pnpm·next.js·dify
假客套12 分钟前
2025 后端自学UNIAPP【项目实战:旅游项目】7、景点详情页面【完结】
前端·uni-app·旅游
寒山李白20 分钟前
Spring Boot面试题精选汇总
java·spring boot·后端·面试
Captaincc26 分钟前
Ilya 现身多大毕业演讲:AI 会完成我们能做的一切
前端·ai编程
teeeeeeemo39 分钟前
Vue数据响应式原理解析
前端·javascript·vue.js·笔记·前端框架·vue
Sahas101943 分钟前
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ is not explicitly defined.
前端·javascript·vue.js
Jinxiansen02111 小时前
Vue 3 实战:【加强版】公司通知推送(WebSocket + token 校验 + 心跳机制)
前端·javascript·vue.js·websocket·typescript
MrSkye1 小时前
React入门:组件化思想?数据驱动?
前端·react.js·面试
BillKu1 小时前
Java解析前端传来的Unix时间戳
java·前端·unix
@Mr_LiuYang1 小时前
网页版便签应用开发:HTML5本地存储与拖拽交互实践
前端·交互·html5·html5便签应用