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++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

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 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt