在UE4虽然系统默认提供的枚举已经很多了,但是做项目的过程中始终会遇到自定义枚举的时候。既可以考虑用蓝图,但更为稳妥的方式是考虑使用C++。
下面将带领大家了解在C++中定义和使用基本的枚举类型,也即UENUM
主要代码如下(为了枚举不冲突,所以推荐使用enum class),官方参考
ABasicStructure.h
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "BasicStructure.generated.h" UENUM(BlueprintType) enum class EMyEnum : uint8 { VE_Dance UMETA(DisplayName = "Dance"), VE_Rain UMETA(DisplayName = "Rain"), VE_Song UMETA(DisplayName = "Song") }; UCLASS() class CPP_API ABasicStructure : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties ABasicStructure(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mantra | Tools") EMyEnum CurrentEnum; };ABasicStructure.cpp
// Fill out your copyright notice in the Description page of Project Settings. #include "BasicStructure.h" // Sets default values ABasicStructure::ABasicStructure() { // 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; CurrentEnum = EMyEnum::VE_Rain; } // Called when the game starts or when spawned void ABasicStructure::BeginPlay() { Super::BeginPlay(); switch (CurrentEnum) { case EMyEnum::VE_Dance: UE_LOG(LogTemp, Warning, TEXT("VE_Dance")); break; case EMyEnum::VE_Rain: UE_LOG(LogTemp, Warning, TEXT("VE_Rain")); break; case EMyEnum::VE_Song: UE_LOG(LogTemp, Warning, TEXT("VE_Song")); break; default: break; } } // Called every frame void ABasicStructure::Tick(float DeltaTime) { Super::Tick(DeltaTime); }