• 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

  • 相关阅读:
    设计模式深度解析:工厂方法模式与抽象工厂模式的深度对比
    18【PreparedStatement接口详细解析】
    springboot对接rabbitmq并且实现动态创建队列和消费
    CentOS7 安装K8S 单节点
    BVR电线与RV电线的区别有哪些?
    使用postMan调试接口出现 Content type ‘multipart/form-data;charset=UTF-8‘ not supported“
    Linux系统中ext3是什么?特点有哪些?
    【NGINX入门指北】 基础篇
    Android Retrofit 封装模版
    JAVA“陶瓷的世界”网页的实际与实现计算机毕业设计Mybatis+系统+数据库+调试部署
  • 原文地址:https://blog.csdn.net/wb175208/article/details/127455240