UE4-對(duì)話系統(tǒng)
事先準(zhǔn)備:創(chuàng)建WidgetBlueprint(控件藍(lán)圖)、MyUserWidget類(lèi)(派生于UserWidget類(lèi))并使其交互绰精,具體方法參考 UE4-UMG與c++交互炮叶。
對(duì)話系統(tǒng)搭建步驟:
- 創(chuàng)建Actor類(lèi)术辐,用來(lái)控制對(duì)話內(nèi)容的顯示。
我創(chuàng)建了WidgetMng.h和WidgetMng.cpp文件拆撼。
- 將自己創(chuàng)建的UserWidget(用戶組件)在游戲開(kāi)始時(shí)加載到視口容劳。
這一步需要分成幾個(gè)小部分:
(1) 在WidgetMng.h中加入2個(gè)變量、將用戶組件加載到視口的方法:
代碼為:
|
public:
/** 在游戲開(kāi)始時(shí)調(diào)用闸度。 */
virtual void BeginPlay() override;
/** 移除當(dāng)前菜單控件并且如果可能竭贩,從指定類(lèi)中創(chuàng)建新控件。 */
UFUNCTION(BlueprintCallable, Category = "UMG Game")
void ChangeMenuWidget(TSubclassOf<UUserWidget> NewWidgetClass);
protected:
/** 在游戲開(kāi)始時(shí)我們將作為菜單使用的控件類(lèi)筋岛。 */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "UMG Game")
TSubclassOf<UUserWidget> StartingWidgetClass;
/** 用作為菜單的控件實(shí)例娶视。 */
UPROPERTY()
UUserWidget* CurrentWidget;
|
(2) 包含所需UserWidget的頭文件
<pre style="margin-left:54.0pt">#include "Blueprint/UserWidget.h"</pre>
(3) 在WidgetMng.cpp中編寫(xiě)方法,這個(gè)方法會(huì)先移除視口中的已存的UserWidget并將我們的UserWidget添加到視口中睁宰。
|
void AWidgetMng::ChangeMenuWidget(TSubclassOf<UMyUserWidget> NewWidgetClass)
{
if (CurrentWidget != nullptr)
{
CurrentWidget->RemoveFromViewport();
CurrentWidget = nullptr;
}
if (NewWidgetClass != nullptr)
{
CurrentWidget = CreateWidget<UMyUserWidget>(GetWorld(), NewWidgetClass);
if (CurrentWidget != nullptr)
{
CurrentWidget->AddToViewport();
}
}
}
|
(4) 在BeginPlay中去調(diào)用
|
void AWidgetMng::BeginPlay()
{
Super::BeginPlay();
ChangeMenuWidget(StartingWidgetClass);
}
|
- 這步開(kāi)始完成關(guān)于定時(shí)器的設(shè)定
(1) 在WidgetMng.h中加入定時(shí)器所需的變量
|
//定時(shí)器相關(guān)參數(shù)
//定時(shí)器句柄
FTimerHandle CountdownTimerHandle;
//定時(shí)器速率
UPROPERTY(EditAnywhere, Category = "Timer")
float Interveral;
|
(2) 在WidgetMng.CPP的BeginPlay中開(kāi)啟定時(shí)器
|
GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &AWidgetMng::UpdateShowContent, Interveral, true);
|
定時(shí)器會(huì)在游戲開(kāi)始時(shí)執(zhí)行肪获,啟用名為CountdownTimerHandle的定時(shí)器,定時(shí)間隔為interval柒傻,循環(huán)定時(shí)孝赫,每次到時(shí)候調(diào)用UpdateShowContent方法。
- 打字機(jī)效果的對(duì)話顯示
(1) 在WidgetMng.h中加入所需變量
|
//對(duì)話系統(tǒng)相關(guān)參數(shù)
//文字索引
int32 index;
//顯示文字初始化
void ShowContInitialize();
//顯示文字更新
void UpdateShowContent();
//用于輸入存放需要顯示內(nèi)容的變量
UPROPERTY(EditAnywhere,Category = "ShowContent")
FString Content;
//用于實(shí)際內(nèi)容顯示的變量
FString ShowCont;
//用于顯示內(nèi)容的矩陣
TCHAR* tmpcont;
|
(2) 在WidgetMng.CPP中編寫(xiě)具體方法
|
void AWidgetMng::UpdateShowContent()
{
if (index < Content.Len())
{
ShowCont += tmpcont[index];
CurrentWidget->MyMenuTitle->SetText(FText::FromString(ShowCont));
index += 1;
}
}
void AWidgetMng::ShowContInitialize()
{
index = 0;
tmpcont = Content.GetCharArray().GetData();
}
|
(3) 在BeginPlay中加入關(guān)于對(duì)話顯示的方法红符,更新后為:
|
void AWidgetMng::BeginPlay()
{
Super::BeginPlay();
ChangeMenuWidget(StartingWidgetClass);
ShowContInitialize();
GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &AWidgetMng::UpdateShowContent, Interveral, true);
}
|
- 回到虛幻引擎編譯文件青柄,將WidgetMng拖入場(chǎng)景中,設(shè)置定時(shí)器的速率预侯,寫(xiě)入要顯示的內(nèi)容致开,查看效果。
初步的打字機(jī)文字效果完成萎馅!