如果想在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