// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/HUD.h"#include"DarkSoulHUD.generated.h"/** * */UCLASS()classDARKSOULSGAME_API ADarkSoulHUD :public AHUD
{
GENERATED_BODY()public:ADarkSoulHUD();//存储主界面UI的模版
TSubclassOf<classUUI_FightMain> FightMainUIClass;//主界面UI的类指针UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category ="MainUI")classUUI_FightMain* FightMainUI;protected:virtualvoidBeginPlay()override;public:inline UUI_FightMain*GetFightMainUI(){
return FightMainUI;};//创建主战斗UIvoidCreateFightMainUI();};
DarkSoulHUD.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"DarkSoulHUD.h"#include"UI/UI_FightMain.h"ADarkSoulHUD::ADarkSoulHUD(){
static ConstructorHelpers::FClassFinder<UUI_FightMain>UI_FightMain(TEXT("/Game/_Game/BP/UI/BP_UI_FightMian"));
FightMainUIClass = UI_FightMain.Class;}voidADarkSoulHUD::BeginPlay(){
创建UI界面//FightMainUI = CreateWidget<UUI_FightMain>(GetWorld(), FightMainUIClass);//if (FightMainUI)//{
// //添加到视口// FightMainUI->AddToViewport(0);//}}voidADarkSoulHUD::CreateFightMainUI(){
//创建UI界面
FightMainUI =CreateWidget<UUI_FightMain>(GetWorld(), FightMainUIClass);if(FightMainUI){
//添加到视口
FightMainUI->AddToViewport(0);}}
DarkSoulPlayerController.h
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/PlayerController.h"#include"DarkSoulPlayerController.generated.h"/** * */UCLASS()classDARKSOULSGAME_API ADarkSoulPlayerController :public APlayerController
{
GENERATED_BODY()public:// Called to bind functionality to input//调用战斗主UIUFUNCTION(BlueprintCallable)voidOpenFightUI();};
DarkSoulPlayerController.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"DarkSoulPlayerController.h"#include"DarkSoulHUD.h"voidADarkSoulPlayerController::OpenFightUI(){
ADarkSoulHUD* HUD =Cast<ADarkSoulHUD>(GetHUD());if(HUD){
//创建主UI
HUD->CreateFightMainUI();}}
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"DarkSoulUserWidgetRule.h"#include"UI_FightResult.generated.h"/** * */UCLASS()classDARKSOULSGAME_API UUI_FightResult :public UDarkSoulUserWidgetRule
{
GENERATED_BODY()public:UUI_FightResult();//相当于BeginPlayvirtualvoidNativeConstruct();//TickvirtualvoidNativeTick(const FGeometry& MyGeometry,float InDeltaTime);//倒计时
int32 CountDownTime;//辅助变量float WaitTime;//返回倒计时UFUNCTION(BlueprintCallable,BlueprintPure)
int32 GetCountDownTime();//设置显示倒计时UFUNCTION(BlueprintImplementableEvent)voidSetCountDownText();//设置胜负UFUNCTION(BlueprintImplementableEvent)voidSetResultText(bool IsWin);};
UI_FightResult.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"UI_FightResult.h"#include"Kismet/GameplayStatics.h"UUI_FightResult::UUI_FightResult(){
}voidUUI_FightResult::NativeConstruct(){
Super::NativeConstruct();
CountDownTime =5;
WaitTime =0;//设置开始倒计时SetCountDownText();}voidUUI_FightResult::NativeTick(const FGeometry& MyGeometry,float InDeltaTime){
Super::NativeTick(MyGeometry, InDeltaTime);if(CountDownTime >0){
WaitTime += InDeltaTime;if(WaitTime >=1){
CountDownTime--;//倒计时SetCountDownText();
WaitTime =0;}//说明五秒过去了,切换地图if(CountDownTime ==0){
UGameplayStatics::OpenLevel(GetWorld(),"TestMainMap");}}}
int32 UUI_FightResult::GetCountDownTime(){
return CountDownTime;}
创建UI实例蓝图
配置战斗结束UI控件并重写蓝图逻辑
配置好UI控件蓝图然后进行编写逻辑
定义战斗结果反馈UI创建方法
DarkSoulHUD.h
在DarkSoulHUD中创建绑定UI方法
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/HUD.h"#include"DarkSoulHUD.generated.h"/** * */UCLASS()classDARKSOULSGAME_API ADarkSoulHUD :public AHUD
{
GENERATED_BODY()public:ADarkSoulHUD();//存储主界面UI的模版
TSubclassOf<classUUI_FightMain> FightMainUIClass;//存储胜负界面UI的模版
TSubclassOf<classUUI_FightResult> FightResultUIClass;//主界面UI的类指针UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category ="MainUI")classUUI_FightMain* FightMainUI;//胜负界面UI的类指针UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category ="MainUI")classUUI_FightResult* FightResultUI;protected:virtualvoidBeginPlay()override;public:inline UUI_FightMain*GetFightMainUI(){
return FightMainUI;};inline UUI_FightResult*GetFightResultUI(){
return FightResultUI;};//创建主战斗UIvoidCreateFightMainUI();//创建胜负UIvoidCreateFightResult(bool IsWin);};
DarkSoulHUD.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"DarkSoulHUD.h"#include"UI/UI_FightMain.h"#include"UI/UI_FightResult.h"ADarkSoulHUD::ADarkSoulHUD(){
static ConstructorHelpers::FClassFinder<UUI_FightMain>UI_FightMain(TEXT("/Game/_Game/BP/UI/BP_UI_FightMian"));static ConstructorHelpers::FClassFinder<UUI_FightResult>UI_FightResult(TEXT("/Game/_Game/BP/UI/BP_UI__FightResult"));
FightMainUIClass = UI_FightMain.Class;
FightResultUIClass = UI_FightResult.Class;}voidADarkSoulHUD::BeginPlay(){
创建UI界面//FightMainUI = CreateWidget<UUI_FightMain>(GetWorld(), FightMainUIClass);//if (FightMainUI)//{
// //添加到视口// FightMainUI->AddToViewport(0);//}}voidADarkSoulHUD::CreateFightMainUI(){
//创建UI界面
FightMainUI =CreateWidget<UUI_FightMain>(GetWorld(), FightMainUIClass);if(FightMainUI){
//添加到视口
FightMainUI->AddToViewport(0);}}voidADarkSoulHUD::CreateFightResult(bool IsWin){
//创建UI界面
FightResultUI =CreateWidget<UUI_FightResult>(GetWorld(), FightResultUIClass);if(FightResultUI){
//调用显示胜负UI的函数
FightResultUI->SetResultText(IsWin);//添加到视口
FightResultUI->AddToViewport(0);}}
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"UObject/NoExportTypes.h"#include"DarkSoulEventManager.generated.h"/** * */UCLASS(BlueprintType)classDARKSOULSGAME_API UDarkSoulEventManager :public UObject
{
GENERATED_BODY()public:UFUNCTION(BlueprintCallable,Category="DarkSoulEventManager")static UDarkSoulEventManager*GetInstancePtr();private://类中定义一个静态指针,指向唯一对象static UDarkSoulEventManager* S_Instance;};
DarkSoulEventManager.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"DarkSoulEventManager.h"//懒汉式单例设计:无创建单例对象,没有在外面创建对象
UDarkSoulEventManager* UDarkSoulEventManager::S_Instance =nullptr;
UDarkSoulEventManager*UDarkSoulEventManager::GetInstancePtr(){
//保证外部只有唯一的对象方式if(S_Instance ==nullptr){
S_Instance =NewObject<UDarkSoulEventManager>();//不被UE系统垃圾回收
S_Instance->AddToRoot();}return S_Instance;}
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"UObject/NoExportTypes.h"#include"DarkSoulEventManager.generated.h"/** * *///创建单播DECLARE_DELEGATE_OneParam(FOpenFightReasultUI,bool);UCLASS(BlueprintType)classDARKSOULSGAME_API UDarkSoulEventManager :public UObject
{
GENERATED_BODY()public:UFUNCTION(BlueprintCallable,Category="DarkSoulEventManager")static UDarkSoulEventManager*GetInstancePtr();private://类中定义一个静态指针,指向唯一对象static UDarkSoulEventManager* S_Instance;public://委托名字
FOpenFightReasultUI OpenFightResultUI;};
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/108306.html