UE4尝试用C++创建蓝图并添加变量

2024-09-06 23:18
文章标签 c++ ue4 创建 尝试 变量 蓝图

本文主要是介绍UE4尝试用C++创建蓝图并添加变量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目标

使用C++创建蓝图资源,并尝试给蓝图添加变量。
(引擎版本4.26)

步骤0. 创建测试用插件

使用编辑器工具栏按钮为模板创建插件。
在这里插入图片描述
使用这个模板的理由是它初始会有个按钮,可以用来触发操作:
在这里插入图片描述
之后将会替换这其中所触发的操作。

步骤1. 创建蓝图

KismetEditorUtilities.h进行include,因为创建蓝图需要用到其中的方法:

#include"Kismet2/KismetEditorUtilities.h"

AssetRegistryModule.h进行include,因为需要通知蓝图资源的创建。

#include"AssetRegistryModule.h"

随后,将按钮触发的操作替换为:

//资源名:
const FString AssetName = "YaksueTestBP";
//package路径名:
const FString PackageName = "/Game/" + AssetName;//创建Package
UPackage * Package = CreatePackage(nullptr, *PackageName);//创建蓝图
UBlueprint* NewBlueprint = FKismetEditorUtilities::CreateBlueprint(AActor::StaticClass(),						//ParentClass					the parent class of the new blueprintPackage,									//Outer							the outer object of the new blueprintFName(*AssetName),							//NewBPName						the name of the new blueprintEBlueprintType::BPTYPE_Normal,				//BlueprintType					the type of the new blueprint (normal, const, etc)UBlueprint::StaticClass(),					//BlueprintClassType			the actual class of the blueprint asset (UBlueprint or a derived type)UBlueprintGeneratedClass::StaticClass(),	//BlueprintGeneratedClassType	the actual generated class of the blueprint asset (UBlueprintGeneratedClass or a derived type)FName("YaksueTestContext"));				//CallingContext				the name of the calling method or module used to identify creation methods to engine analytics/usage stats (default None will be ignored)//通知这个蓝图资源创建了
FAssetRegistryModule::AssetCreated(NewBlueprint);//编译蓝图
FKismetEditorUtilities::CompileBlueprint(NewBlueprint);

效果:点击按钮后会创建一个空白的蓝图:
在这里插入图片描述

步骤2. 为蓝图添加变量

关于“添加变量”这个功能,我翻阅代码找到了 FBlueprintEditorUtils::AddMemberVariable 接口。

通过字符串搜索,我找到了一处使用范例,在\UE_4.26\Engine\Plugins\Tests\EditorTests\Source\EditorTests\Private\UnrealEd\EditorBuildPromotionTests.cpp
在这里插入图片描述
关于变量的类型,通过UEdGraphSchema_K2::PC_String指定为“字符串”。


需要添加BlueprintGraph这个模块到TestCppBP.Build.csPrivateDependencyModuleNames中。因为UEdGraphSchema_K2::PC_String等其他的类型实际是个名字,为 static:
在这里插入图片描述
其值在EdGraphSchema_K2.cpp中指定:
在这里插入图片描述
如果不添加BlueprintGraph这个模块,UEdGraphSchema_K2::PC_String等的值无法知道。


仿照范例添加字符串变量:

FEdGraphPinType StringPinType(UEdGraphSchema_K2::PC_String,		//PinCategory				Category of pin typeNAME_None,							//PinSubCategory			Sub-category of pin typenullptr,							//PinSubCategoryObject		Sub-category objectEPinContainerType::None,			//ContainerTypefalse,								//bIsReference				Whether or not this pin is a value passed by reference or notFEdGraphTerminalType());			//PinValueType				Data used to determine value types when bIsMap is trueFBlueprintEditorUtils::AddMemberVariable(NewBlueprint, "TestString", StringPinType);

效果,成功添加:
在这里插入图片描述

最终代码

...
#include"Actor.h"
#include"Kismet2/KismetEditorUtilities.h"
#include"Kismet2/BlueprintEditorUtils.h"
#include"AssetRegistryModule.h"
...
void FTestCppBPModule::PluginButtonClicked()
{//资源名:const FString AssetName = "YaksueTestBP";//package路径名:const FString PackageName = "/Game/" + AssetName;//创建PackageUPackage * Package = CreatePackage(nullptr, *PackageName);//创建蓝图UBlueprint* NewBlueprint = FKismetEditorUtilities::CreateBlueprint(AActor::StaticClass(),						//ParentClass					the parent class of the new blueprintPackage,									//Outer							the outer object of the new blueprintFName(*AssetName),							//NewBPName						the name of the new blueprintEBlueprintType::BPTYPE_Normal,				//BlueprintType					the type of the new blueprint (normal, const, etc)UBlueprint::StaticClass(),					//BlueprintClassType			the actual class of the blueprint asset (UBlueprint or a derived type)UBlueprintGeneratedClass::StaticClass(),	//BlueprintGeneratedClassType	the actual generated class of the blueprint asset (UBlueprintGeneratedClass or a derived type)FName("YaksueTestContext"));				//CallingContext				the name of the calling method or module used to identify creation methods to engine analytics/usage stats (default None will be ignored)//通知这个蓝图资源创建了FAssetRegistryModule::AssetCreated(NewBlueprint);//添加一个字符串类型的变量{FEdGraphPinType StringPinType(UEdGraphSchema_K2::PC_String,		//PinCategory				Category of pin typeNAME_None,							//PinSubCategory			Sub-category of pin typenullptr,							//PinSubCategoryObject		Sub-category objectEPinContainerType::None,			//ContainerTypefalse,								//bIsReference				Whether or not this pin is a value passed by reference or notFEdGraphTerminalType());			//PinValueType				Data used to determine value types when bIsMap is trueFBlueprintEditorUtils::AddMemberVariable(NewBlueprint, "TestString", StringPinType);}//编译蓝图FKismetEditorUtilities::CompileBlueprint(NewBlueprint);
}
...

这篇关于UE4尝试用C++创建蓝图并添加变量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1143400

相关文章

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

Java中对象的创建和销毁过程详析

《Java中对象的创建和销毁过程详析》:本文主要介绍Java中对象的创建和销毁过程,对象的创建过程包括类加载检查、内存分配、初始化零值内存、设置对象头和执行init方法,对象的销毁过程由垃圾回收机... 目录前言对象的创建过程1. 类加载检查2China编程. 分配内存3. 初始化零值4. 设置对象头5. 执行

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Python创建Excel的4种方式小结

《Python创建Excel的4种方式小结》这篇文章主要为大家详细介绍了Python中创建Excel的4种常见方式,文中的示例代码简洁易懂,具有一定的参考价值,感兴趣的小伙伴可以学习一下... 目录库的安装代码1——pandas代码2——openpyxl代码3——xlsxwriterwww.cppcns.c

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解