原文鏈接:http://gad.qq.com/article/detail/7181131
本文首發騰訊GAD開發者平台,未經允許,不得轉載
UE4的藍圖之強大讓人欲罷不能,但是實際在項目的開發中,C++與藍圖都需要結合使用,單獨選擇一樣開發都不是特別科學,這里我就來研究了一下C++使用UMG接口來操作界面,我的目的非常簡單,用C++來創建界面,並在創建成功的時候,告訴界面打印出相關信息。
1.創建一個C++的空模板工程,命名UMGProject,用VS打開工程文件,找到UMGProject.Build.cs,在PublicDependencyModuleNames后面添加UMG模塊,並取消PrivateDependencyModuleNames的注釋,並編譯工程。
using UnrealBuildTool;
public class UMGProject : ModuleRules
{
public UMGProject(TargetInfo Target)
{
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore","UMG" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
2.回到UE4的工程中,在Content下創建一個Blueprint的文件夾,在Blueprint下右鍵空白處點擊選擇User Interface/Wiget Blueprint,命名BP_UI
3.雙擊打開BP_UI,在中間添加一個Text控件,並設置為顯眼一點的紅色,並設置它的IsVariable為true,后面再創建函數的時候才方便使用。
4.並且我在BP_UI里面創建一個函數接口,在右上角點擊Graph,添加一個Functions,命令為ShowMessage,功能就是用Text顯示傳送過來的信息。
5.在C++ Classes下面創建一個新的Class,繼承自PlayerController,命名為UMGPlayerController,用VS打開后,我們需要在UMGPlayerController.h文件中的UCLASS里面添加Abstract和Blueprintable,UCLASS表示類修飾符,里面的參數是類具體的表述。
Abstract 類修飾符將類聲明為“抽象基類”,這樣會阻止用戶在虛幻編輯器中向這個世界中添加這個類的Actor,或者在游戲過程中創建這個類的實例,而
Blueprintable
指定該類為創建藍圖的可接受基類。除非被繼承,否則默認值為NotBlueprintable。它由子類繼承。
同時在這個類中,我們還會用到一個函數修飾符,BlueprintImplementableEvent,表示
此函數可以在藍圖或關卡藍圖圖表內進行重載。
這里就是用於C++與藍圖的交互接口,同時這個函數不用在cpp中再去實現它,完成后編譯。
#include "GameFramework/PlayerController.h"
#include "UMGPlayerController.generated.h"
/**
*
*/
UCLASS(Abstract, Blueprintable)
class UMGPROJECT_API AUMGPlayerController : public APlayerController
{
GENERATED_BODY()
public:
/**此函數可以在藍圖或關卡藍圖圖表內進行重載*/
UFUNCTION(BlueprintImplementableEvent, Category = "UMG")
void PrintMessage(const FString &Message);
};
6.在ue4中,Content/Blueprint下創建一個新的藍圖繼承自上面所寫的UMGPlayerController,命名為BP_UMGController。
7.我們再打開C++下面的UMGProjectGameMode,詳細解釋,都寫在了注釋中,主要在C++的頂上要添加UMG.h的頭文件,在UMG.h包括了UMG整個模塊的所有頭文件,所以我們只添加它一個就可以了,在完成后,編譯一下。
頭文件:
#pragma once
#include "UMG.h"
#include "GameFramework/GameMode.h"
#include "UMGProjectGameMode.generated.h"
/**
*
*/
UCLASS()
class UMGPROJECT_API AUMGProjectGameMode : public AGameMode
{
GENERATED_BODY()
public:
AUMGProjectGameMode();
~AUMGProjectGameMode();
virtual void BeginPlay() override;
//獲取菜單
UFUNCTION(BlueprintCallable, Category = "UMG")
UUserWidget* GetCountWidget();
protected:
//菜單
UPROPERTY()
UUserWidget* CountWidget;
//創建菜單
void CreateCountWidget();
};
源文件:
#include "UMGProject.h"
#include "UMGProjectGameMode.h"
#include "UMGPlayerController.h"
AUMGProjectGameMode::AUMGProjectGameMode()
{
//通過路徑找到藍圖,並將藍圖控制器的類設置給PlayerControllerClass
static ConstructorHelpers::FClassFinder<AUMGPlayerController> UMGControllerClassFinder(TEXT("/Game/Blueprint/BP_UMGController"));
PlayerControllerClass = UMGControllerClassFinder.Class;
CreateCountWidget();
}
AUMGProjectGameMode::~AUMGProjectGameMode()
{
}
void AUMGProjectGameMode::BeginPlay()
{
Super::BeginPlay();
//獲取PlayerController並調用開始所寫的PrintMessage函數
Cast<AUMGPlayerController>(GetWorld()->GetFirstPlayerController())->PrintMessage(TEXT("開始游戲~~~"));
}
UUserWidget* AUMGProjectGameMode::GetCountWidget()
{
//返回計數界面
return CountWidget;
}
void AUMGProjectGameMode::CreateCountWidget()
{
//通過路徑找到藍圖
static ConstructorHelpers::FClassFinder<UUserWidget> UMGClassFinder(TEXT("/Game/Blueprint/BP_UI"));
//通過藍圖的類,創建菜單
CountWidget = CreateWidget<UUserWidget>(GetWorld(), UMGClassFinder.Class);
//添加到視圖中
if (CountWidget != nullptr)
CountWidget->AddToViewport();
}
8.再打開ue4的BP_UMGController,點擊Functions的Override,就會看到我們所寫的PrintMessage
然后點擊PrintMessage鏈接以下藍圖,就是在調用PrintMessage的時候,我們再去調用UI的ShowMessage,將傳來的Message顯示出來。
9.編譯運行,運行順序差不多就是在GameMode的構造函數里面設置PlayerController,然后通過路徑找到並創建我們的BP_UI,然后再在BeginPlay里面輸出文字信息。
感覺繞了一大圈,做了一個特別小的功能,不過這里主要也是為了表現C++使用UMG的接口,和C++調用藍圖的函數,在實際開發中C++與藍圖的交互是相當重要的。
