c++获取umg ue_[UE4][V4.10]C++中定义UMG widget变量时的头文件引用有关问题

在UE4.10版本中,使用C++定义UUserWidget和UMG Component变量时,需要正确引用头文件。对于UUserWidget,仅需包含"Blueprint/UserWidget.h";对于UButton等UMG Component,需包含多个头文件。同时,在"工程名.Build.cs"中添加UMG、Slate和CoreSlate依赖。不正确的头文件引用会导致编译错误。
摘要由CSDN通过智能技术生成

如果想在c++中定义UUserWidget类型的变量,比如在PlayerController头文件中如下定义:

protected:

/** The widget class we will use as our game over screen when the player wins. */

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Game")

TSubclassOf VictoryWidgetClass;

/** The widget instance that we are using as our menu. */

UPROPERTY()

UUserWidget* CurrentWidget;

那么在V4.10版本中需要引用的头文件只需加入:

#include "Blueprint/UserWidget.h"

以前的旧版本,需要这样加入:

#include "Runtime/UMG/Public/UMG.h"

#include "Slate.h"

如果在4.10版本中加入上面两行,则会编译错误:

Error C2440 'initializing': cannot convert from 'const char [106]' to 'int'

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int

另外别忘了在你的“工程名.Build.cs”构建配置中加入UMG、Slate和CoreSlate:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore"});

=====================================================

如果想在C++代码中定义UMG Component类型的变量,比如一个UButton:

UButton* btn = Cast(CanvasPanelWidget->GetChildAt(0))

那么还需要在你的“工程名.h”中加入以下头文件:

#include "Runtime/UMG/Public/UMG.h"

#include "Runtime/UMG/Public/UMGStyle.h"

#include "Runtime/UMG/Public/Slate/SObjectWidget.h"

#include "Runtime/UMG/Public/IUMGModule.h"

#include "Runtime/UMG/Public/Blueprint/UserWidget.h"

否则会出现编译错误:

error C2504: 'UContentWidget': base class undefined

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值