当前位置: 首页 > 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;
}

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

http://www.lryc.cn/news/431106.html

相关文章:

  • C语言行地址列地址区别,内存的分配
  • Unity 一键修改图片缩放保存为当前的一半大小
  • Identifying User Goals from UI Trajectories论文学习
  • [STM32]从零开始的STM32标准库环境搭建(小白向)
  • 解决 Android 上的 .NET MAUI/Xamarin.AndroidX 应用调用 ASP.NET Core API 端点时 SSL 连接被拒绝的问题
  • 助贷CRM系统:为金融中介行业打造全新营销管理模式
  • HBase 部署及shell操作
  • 使用Fign进行客户端远程调用和SpringFormEncoder的使用
  • golang 数据库使用注意事项
  • Leetcode面试经典150题-221.最大正方形
  • 51单片机-DS1302,操作简述
  • Vue3+Vite+Echarts 出现Missing semicolon错误
  • iOS——frame和bounds的区别
  • Trm理论 3(注意力机制)
  • Vue2和Vue3项目创建的区别和 element ui 和element plus的导入方式
  • 基于STM32的猫狗宠物喂养系统设计(微信小程序)(215)
  • spark读取csv文件
  • 钢铁百科:Q420DR力学性能、Q420DR执行标准、Q420DR低温容器钢板
  • 三菱机器人手柄维修示教器维修手操器面板等
  • 中间件的学习理解总结
  • 编程秘密武器:提升工作效率的关键工具
  • Git+word记笔记
  • java-antrl手敲命令的hello world
  • 法规探讨 | 《医疗器械管理法(草案征求意见稿)》初探(1)
  • 大语言模型的上下文窗口(Context Windows):对人工智能应用的影响
  • Java【数组】
  • xAI巨无霸超级计算机上线:10万张H100 GPU,计划翻倍至20万张
  • python集合
  • 算法打卡 Day29(回溯算法)-复原 IP 地址 + 子集 + 子集 Ⅱ
  • LeetCode 热题100-17 缺失的第一个正数