如何在 Unreal Engine 4.26 C++ 中使用 'UWidgetLayoutLibrary::GetMousePositionOnViewport(...)'?

How to use 'UWidgetLayoutLibrary::GetMousePositionOnViewport(...)' in Unreal Engiene 4.26 C++?

我创建了实现 AActor 的简单 class。在此 class 中,我想在游戏显示中获取鼠标位置。我知道,我必须在 *.cpp 文件中包含 'Blueprint/WidgetLayoutLibrary.h' 并在方法 'Tick' 中写入以下内容:

FVector2D mousePosition = UWidgetLayoutLibrary::GetMousePositionOnViewport(GetWorld());

这样每次滴答都能得到鼠标位置... 但!!!我得到一个错误:

 Worlds_constantes.cpp.obj : error LNK2019: ??뫪? ?? ??ࠧ??襭??? ???譨? ᨬ??? "__declspec(dllimport) public: static struct FVector2D __cdecl UWidgetLayoutLibrary::GetMousePositionOnViewport(class UObject *)" (__imp_?GetMousePositionOnViewport@UWidgetLayoutLibrary@@SA?AUFVector2D@@PEAVUObject@@@Z) ? ?㭪樨 "public: virtual void __cdecl AWorlds_constantes::Tick(float)" (?Tick@AWorlds_constantes@@UEAAXM@Z).
 C:\Users\user\Documents\Unreal Projects\Project_name\Binaries\Win64\UE4Editor-Project_name-6913.dll : fatal error LNK1120: ??ࠧ??襭??? ???譨? ??????⮢: 1

怎么办?如何解决?

也许你现在已经找到答案了,但是这个问题已经困扰我好几天了,所以我决定在这里回复你,只需在"UMG" specified position in Project.build.cs,可能这个问题太简单了,几乎没有答案online.But刚学Unreal和C++的不知道.

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