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

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

相关推荐
萤丰信息4 分钟前
智慧园区能源革命:从“耗电黑洞”到零碳样本的蜕变
java·大数据·人工智能·科技·安全·能源·智慧园区
曹牧30 分钟前
Eclipse为方法添加注释
java·ide·eclipse
小满zs1 小时前
Next.js第十一章(渲染基础概念)
前端
我叫张小白。1 小时前
Spring Boot拦截器详解:实现统一的JWT认证
java·spring boot·web·jwt·拦截器·interceptor
不羁的fang少年2 小时前
前端常见问题(vue,css,html,js等)
前端·javascript·css
change_fate2 小时前
el-menu折叠后文字下移
前端·javascript·vue.js
yivifu2 小时前
CSS Grid 布局详解(2025最新标准)
前端·css
Gerardisite3 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
o***Z4484 小时前
前端性能优化案例
前端
张拭心4 小时前
前端没有实际的必要了?结合今年工作内容,谈谈我的看法
前端·ai编程