C++ 在 Unreal 中为游戏增加实时音视频互动的教程详解

2020-05-29 11:02:26于丽

同时,还要添加如下成员:

//EnterChannelWidget.h
...
UCLASS()
class AGORAVIDEOCALL_API UEnterChannelWidget : public UUserWidget
{
 GENERATED_BODY()
 ...
public:
 AVideoCallPlayerController* PlayerController = nullptr;
 TUniquePtr<VideoCall> VideoCallPtr;
 ...
};

添加 Constructor 和 Construct/Destruct 方法

//EnterChannelWidget.h


...


UCLASS()
class AGORAVIDEOCALL_API UEnterChannelWidget : public UUserWidget
{
 GENERATED_BODY()


public:


 ...


 UEnterChannelWidget(const FObjectInitializer& objectInitializer);


 void NativeConstruct() override;


 ...
};
//EnterChannelWidget.cpp


UEnterChannelWidget::UEnterChannelWidget(const FObjectInitializer& objectInitializer)
 : Super(objectInitializer)
{
}


void UEnterChannelWidget::NativeConstruct()
{
 Super::NativeConstruct();


 if (HeaderTextBlock)
 HeaderTextBlock->SetText(FText::FromString("Enter a conference room name"));


 if (DescriptionTextBlock)
 DescriptionTextBlock->SetText(FText::FromString("If you are the first person to specify this name, 
the room will be created and you willnbe placed in it. 
If it has already been created you will join the conference in progress"));


 if (ChannelNameTextBox)
 ChannelNameTextBox->SetHintText(FText::FromString("Channel Name"));


 if (EncriptionKeyTextBox)
 EncriptionKeyTextBox->SetHintText(FText::FromString("Encription Key"));


 if (EncriptionTypeTextBlock)
 EncriptionTypeTextBlock->SetText(FText::FromString("Enc Type:"));


 if (EncriptionTypeComboBox)
 {
 EncriptionTypeComboBox->AddOption("aes-128");
 EncriptionTypeComboBox->AddOption("aes-256");
 EncriptionTypeComboBox->SetSelectedIndex(0);
 }


 if (JoinButton)
 {
 UTextBlock* JoinTextBlock = WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass());
 JoinTextBlock->SetText(FText::FromString("Join"));
 JoinButton->AddChild(JoinTextBlock);
 JoinButton->OnClicked.AddDynamic(this, &UEnterChannelWidget::OnJoin);
 }


 if (ContactsTextBlock)
 ContactsTextBlock->SetText(FText::FromString("agora.io Contact support: 400 632 6626"));


 if (BuildInfoTextBlock)
 BuildInfoTextBlock->SetText(FText::FromString(" "));
}

增加 Setter 方法

初始化 PlayerController 和 VideoCallPtr 变量

//EnterChannelWidget.h


...


UCLASS()
class AGORAVIDEOCALL_API UEnterChannelWidget : public UUserWidget
{
 GENERATED_BODY()


public:


 ...


 void SetVideoCallPlayerController(AVideoCallPlayerController* VideoCallPlayerController);


 void SetVideoCall(TUniquePtr<VideoCall> PassedVideoCallPtr);


 ...
};
//EnterChannelWidget.cpp


void UEnterChannelWidget::SetVideoCallPlayerController(AVideoCallPlayerController* VideoCallPlayerController)
{
 PlayerController = VideoCallPlayerController;
}


void UEnterChannelWidget::SetVideoCall(TUniquePtr<VideoCall> PassedVideoCallPtr)
{
 VideoCallPtr = std::move(PassedVideoCallPtr);
}

增加 BlueprintCallable方法