ue4

Widgetと画面遷移


level遷移の実装

レベルの遷移の実装をします。
ボタンをクリックすると別レベルの遷移するようにします。

遷移レベルの作成

遷移用にレベルを2つ作成します。
2つのレベルには同じレベルスクリプト

を設定します。

map0
vatico

map1
vatico

レベルにフェードインを登録

遷移時にフェードインするようにします。
ALevelScriptActorBaseでフェードを登録し、
フェードインできるようにします。

BeginPlay()でフェードを作成(AWgFade::Create)し
WgFade->StartFade()でフェードを開始します。

    auto WgFade = AWgFade::Create(this);
    if (WgFade) {
        WgFade->StartFade();
    }

ALevelScriptActorBase.cpp

//=======================================================================
///  @file ALevelScriptActorBase.cpp
///  @brief ALevelScriptActorBase
///  @author     inuvatico
///  @date        2021/04/02
///  @version     1.0
///  @copyright   2020 inuvatico
///  Released under the MIT license.
///    see https://opensource.org/licenses/MIT
///  @par (new/Add/Change : 2020/10/17) 
//=======================================================================
#include "LevelScriptActorBase.h"

#include "App/UI/TestMenu.h"
#include "App/UI/Fade/WgFade.h"
#include "GameInstance/sub/GInstSubInstanceMng.h"

//==============================================
//! BeginPlay
//==============================================
void ALevelScriptActorBase::BeginPlay() {
    Super::BeginPlay();
    ATestMenu::Create(this);
    auto WgFade = AWgFade::Create(this);
    if (WgFade) {
        WgFade->StartFade();
    }
}

//==============================================
//! EndPlay
//==============================================
void ALevelScriptActorBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
    Super::EndPlay(EndPlayReason);
    UGInstSubInstanceMng::Get()->ClearWidget();
}

ボタンにフェードアウトとレベル遷移処理を追加

ATestMenuのOnActionにフェードアウトと遷移処理を追加します。
遷移はフェードアウト後にコールバックとして実行します。

//=====================================================
//! OnAction
//=====================================================
bool ATestMenu::OnAction(FString Action, int32 Param0, int32 Param1) {

    if (Action == "OnClicked") {
        //--------------------------------------------------
        //Fadeのコールバックをラムダで作成
        //--------------------------------------------------
        TFunction<void(void)> CallBackFunc = nullptr;
        {
            static int test = 1;
            if (test & 1) {
                //"Map01"に遷移
                CallBackFunc = [this] {UGameplayStatics::OpenLevel(GetWorld(), "Map01"); };
            }
            else {
                //"Map00"に遷移
                CallBackFunc = [this] {UGameplayStatics::OpenLevel(GetWorld(), "Map00"); };
            }
            test++;
        }

        //--------------------------------------------------
        //Fadeの開始
        //--------------------------------------------------
        AWgFade* Fade = Cast<AWgFade>(UGInstSubInstanceMng::Get()->GetWidget(UIE_FADE));
        if (Fade) {
            Fade->SetCallBack(CallBackFunc);
            Fade->StartFade(1.0f);
        }
        return true;
    }
    return false;
}

TestMenu.cpp

//=======================================================================
///  @file TestMenu.cpp
///  @brief  ウィジェットのテスト
///  @author     inuvatico
///  @date        2021/04/02
///  @version     1.0
///  @copyright   2020 inuvatico
///  Released under the MIT license.
///    see https://opensource.org/licenses/MIT
///  @par (new/Add/Change : 2020/10/17) 
//=======================================================================
#include "TestMenu.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
#include "Res/ResDef.h"
#include "App/UI/Fade/WgFade.h"
#include "GameInstance/sub/GInstSubInstanceMng.h"
#include "UI/UIDef.h"

#pragma optimize("", off) 

//=====================================================
//! メニューの作成
//=====================================================
ATestMenu* ATestMenu::Create(UObject* Obj) {

    ATestMenu* Wg =  Cast<ATestMenu>(UGInstSubInstanceMng::Get()->GetWidget(UIE_TEST_MENU, false));
    if (!Wg) {

        Wg = Obj->GetWorld()->SpawnActor<ATestMenu>(ATestMenu::StaticClass());
        UGInstSubInstanceMng::Get()->SetWidget(UIE_TEST_MENU, Wg);

        // ウィジェットの読み込み
        {
            FString path = UIR_TEST_MENU;
            TSubclassOf<class UUserWidget> WidgetClass;
            WidgetClass = TSoftClassPtr<UUserWidget>(FSoftObjectPath(*path)).LoadSynchronous();
            UUserWidget* UserWidget = CreateWidget<UUserWidget>(Obj->GetWorld(), WidgetClass);
            UserWidget->AddToViewport(ZO_MENU);
            //GEngine->AddOnScreenDebugMessage(-1, -1, FColor::Orange, FString::Printf(TEXT("AMyLevelScriptActor")));
        }
    }
    return Wg;
}

//=====================================================
//! OnAction
//=====================================================
bool ATestMenu::OnAction(FString Action, int32 Param0, int32 Param1) {
    UE_LOG(LogTemp, Log, TEXT("ATestMenu:%s"), *Action);
    GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Orange, FString::Printf(TEXT("ATestMenu:%s"), *Action));

    if (Action == "OnClicked") {
        //--------------------------------------------------
        //Fadeのコールバックをラムダで作成
        //--------------------------------------------------
        TFunction<void(void)> CallBackFunc = nullptr;
        {
            static int test = 1;
            if (test & 1) {
                CallBackFunc = [this] {UGameplayStatics::OpenLevel(GetWorld(), "Map01"); };
            }
            else {
                CallBackFunc = [this] {UGameplayStatics::OpenLevel(GetWorld(), "Map00"); };
            }
            test++;
        }

        //--------------------------------------------------
        //Fadeの開始
        //--------------------------------------------------
        AWgFade* Fade = Cast<AWgFade>(UGInstSubInstanceMng::Get()->GetWidget(UIE_FADE));
        if (Fade) {
            Fade->SetCallBack(CallBackFunc);
            Fade->StartFade(1.0f);
        }
        return true;
    }
    return false;
}
prev/next







digitalize
  始めました。