• UE4切换关卡


    当设计完成一个场景中的游戏的时候或者角色在一个场景中结束战斗后,需要切换到另外一个场景中,这就需要场景的切换了。
    新建一个场景切换的Box,当角色和Box发生碰撞的时候,就会自动加载另外一个场景。
    首先定义这个Box类:ALevelTranslateVolume

    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "GameFramework/Actor.h"
    #include "LevelTranslateVolume.generated.h"
    
    UCLASS()
    class FIRSTPROJECT_API ALevelTranslateVolume : public AActor {
    	GENERATED_BODY()
    
    public:
    	// Sets default values for this actor's properties
    	ALevelTranslateVolume();
    
    	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Translate")
    	class UBoxComponent* levelTranslateVolume = nullptr;
    
    	class UBillboardComponent* billboard = nullptr;
    
    	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Translate")
    	FName newLevelName;
    
    protected:
    	// Called when the game starts or when spawned
    	virtual void BeginPlay() override;
    
    public:
    	// Called every frame
    	virtual void Tick(float DeltaTime) override;
    
    	UFUNCTION()//如果不添加这个宏,函数就不起作用
    	virtual void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    // Fill out your copyright notice in the Description page of Project Settings.
    
    
    #include "LevelTranslateVolume.h"
    #include "Components/BoxComponent.h"
    #include "Components/BillboardComponent.h"
    #include "MainCharacter.h"
    
    // Sets default values
    ALevelTranslateVolume::ALevelTranslateVolume() {
    	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    	PrimaryActorTick.bCanEverTick = true;
    
    	levelTranslateVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("LevelTranslate"));
    	RootComponent = levelTranslateVolume;
    
    	billboard = CreateDefaultSubobject<UBillboardComponent>(TEXT("Billboard"));
    	billboard->SetupAttachment(GetRootComponent());
    
    	newLevelName = "SunTemple";
    }
    
    // Called when the game starts or when spawned
    void ALevelTranslateVolume::BeginPlay() {
    	Super::BeginPlay();
    
    	levelTranslateVolume->OnComponentBeginOverlap.AddDynamic(this, &ALevelTranslateVolume::onBeginOverlap);
    
    }
    
    // Called every frame
    void ALevelTranslateVolume::Tick(float DeltaTime) {
    	Super::Tick(DeltaTime);
    
    }
    
    void ALevelTranslateVolume::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
    	if (OtherActor){
    		AMainCharacter* mainCharacter = Cast<AMainCharacter>(OtherActor);
    		if (mainCharacter){
    			mainCharacter->switchLevel(newLevelName);
    		}
    	}
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在角色类中设置切换开关的接口:

    //根据名称切换不同的关卡
    void switchLevel(FName name);
    
    • 1
    • 2
    void AMainCharacter::switchLevel(FName name) {
    	UWorld* world = GetWorld();
    	if (world){
    		FString mapName = world->GetMapName();
    		FName currentMapName(*mapName);
    		if (currentMapName != name){
    			UGameplayStatics::OpenLevel(world, name);
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    创建ALevelTranslateVolume对应的蓝图类,并且把创建好的蓝图类拖动到场景中,并且设置新地图名称
    在这里插入图片描述

    aaa

  • 相关阅读:
    推荐计划里何时使用经常性与一次性推荐奖励?
    2022 年-Q2
    一文简要了解星闪技术优势点
    第5章相似矩阵及二次型(4)
    刚考过PMP想问一下怎样才能转行做PM
    扩展欧几里得
    JVM学习-字节码指令集(三)
    Canvas 从 0 到 1 -- 开发 2D 游戏《保卫家园》-- 【上篇】
    黄金现货期货各有各的市场
    mysql基础语法速成版
  • 原文地址:https://blog.csdn.net/wb175208/article/details/127455240