当前位置: 首页 > news >正文

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

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

	static TSharedPtr<IImageWrapper> GetImageWrapperByExtention(const FString InImagePath);  //智能指针,方便追寻引用C++,加载ImageWrapperstatic 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里,添加需要使用的头文件。

#include "IImageWrapper.h"      //图像信息的方法#include "IImageWrapperModule.h"   //图片相关信息的模块#include "Engine/Texture2D.h"#include "CoreMinimal.h"   //#include "Kismet/BlueprintFunctionLibrary.h"

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

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的类一样声明如下,可读可写。图片高和宽。

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++。在虚幻进行 引用计数 和 垃圾回收 很方便追寻,并且平台融合很好。

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++,加载ImageWrapperstatic UTexture2D* LoadTexture2D(const FString& ImagePath, bool& IsValid, int32& OutWidth, int32& OutHeight); //加Const 为输入参数 ,不加为输出参数};

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

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

通过文件路径,去加载图片。如果想要使用

FPlatformFileManager::Get().GetPlatformFile().FileExists(*ImagePath)

则需要这些头文件,在CPP中:

#include "Misc/FileHelper.h"   //文件相关的头文件#include "Misc/Paths.h" // 路径相关头文件 //上面两个在其它读取文件也常用到#include "Modules/ModuleManager.h" //模块相关头文件#include "HAL/PlatformFileManager.h"  //平台相关的头文件

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


http://www.mrgr.cn/news/19382.html

相关文章:

  • 怎么打黑神话悟空里的隐藏BOSS金池长老,黑风山关键关卡之一
  • ansible+awx搭建
  • 吐血整理(最全论文指令手册),还有 ChatGPT 3.5/4.0 新手使用手册~ 【亲测好用】
  • vue3项目 / 三子棋
  • VMware 虚拟化平台部分问题和优化措施汇总
  • 软件测试工具——Junit单元测试
  • 【JavaScript】标准库:Array对象
  • 构建现代前端应用的利器:深入解析Webpack与Vite的差异与优势
  • 出现 /www/server/mysql/bin/mysqld: Shutdown complete 的解决方法
  • 【hot100篇-python刷题记录】【跳跃游戏 II】
  • 后端微服务架构下的服务数据隔离:Schema与Tenant
  • 坑——fastjson将字符串转到带枚举的java对象
  • 微小目标检测
  • 【2025考研英语高分写作:写作核心词汇】四、社会热点
  • 【C++ 第二十章】模拟实现 shared_ptr(可以拷贝的智能指针)
  • 大屏适配各分辨率屏幕方案及整合动画性能
  • 公式编辑支持SUBSTITUTE()函数
  • 一起学习LeetCode热题100道(70/100)
  • 2024年住宅代理市场概况:趋势与选择指南
  • Robotics: computational motion planning 部分笔记—— week 1 graph-based