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

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

相关推荐
小白小白从不日白35 分钟前
react 组件化开发_生命周期_表单处理
前端·react.js
weixin_417759991 小时前
97-java流式处理优点
java·开发语言
AskHarries1 小时前
Spring Boot集成Akka Cluster实现在分布式节点中执行任务
java·spring boot·maven·akka
计算机编程-吉哥1 小时前
计算机毕业设计 网上书店系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
java·spring boot·毕业设计·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·网上书店系统
我爱豆子1 小时前
Leetcode Hot 100刷题记录 -Day14(矩阵置0)
java·算法·leetcode·矩阵
程序员古德1 小时前
《论负载均衡技术在Web系统中的应用》写作框架,软考高级系统架构设计师
前端·系统架构·负载均衡
方圆师兄1 小时前
k8s 常见问题梳理
java·kubernetes
骆晨学长1 小时前
基于Springboot的校园防疫管理系统的设计与实现
java·spring boot·后端
java1234_小锋1 小时前
免费分享一套SpringBoot+Vue学生信息管理系统【论文+源码+SQL脚本】,帅呆了~~
java·springboot·java毕业设计·学生信息·java学生信息·springboot学生信息·vue学生信息
小白小白从不日白2 小时前
TS axios封装
前端·typescript