UE/C++简单功能实现笔记

本篇笔记主要用于记录如何利用C++在虚幻引擎5中实现一些基本的功能需求。

实现功能与代码

以下代码均来自我的跳棋小游戏,所以默认Pawn的类名为ATiaoQi

构造函数中添加物体

.h

UCLASS()
class UNREALTIAOQI_API ATiaoQi : public APawn
{
    GENERATED_BODY()
public:
    UStaticMeshComponent* selectCircle;
    ATiaoQi();
}

.cpp

ATiaoQi::ATiaoQi() {
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    selectCircle = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("selectCircle"));
    static ConstructorHelpers::FObjectFinder<UStaticMesh> PlaneAsset(TEXT("/Script/Engine.StaticMesh'/Engine/BasicShapes/Plane.Plane'"));
    static ConstructorHelpers::FObjectFinder<UMaterial> PlaneMaterial(TEXT("/Script/Engine.Material'/Game/Material/selectBall.selectBall'"));  //右键材质,复制引用即可获得该字符串
    selectCircle->SetStaticMesh(PlaneAsset.Object);
    selectCircle->SetMaterial(0, PlaneMaterial.Object);
    const double selectCircleSize = 0.5;
    selectCircle->SetWorldScale3D(FVector(selectCircleSize, selectCircleSize, selectCircleSize));
    selectCircle->SetVisibility(false); //隐藏物体
}

运行时添加C++ Actor

.h

class ACBall;
UCLASS()
class UNREALTIAOQI_API ATiaoQi : public APawn
{
    GENERATED_BODY()
public:
    ACBall* middleBall;
}

.cpp

#include "CBall.h"
const FTransform SpawnLocAndRotation(...) //省略具体值;
middleBall = mainWorld->SpawnActorDeferred<ACBall>(ACBall::StaticClass(), SpawnLocAndRotation); //添加了ACBall类C++ Actor

运行时设置动态材质及参数

.h

class UNREALTIAOQI_API ATiaoQi : public APawn
{
    GENERATED_BODY()
public:
    UStaticMeshComponent* playerTriangle;
    UMaterialInterface* triangleStaticMaterial;
    UMaterialInstanceDynamic* DynMaterial;
}

.cpp

ATiaoQi::ATiaoQi() {
    static ConstructorHelpers::FObjectFinder<UMaterial> TrianMaterial(TEXT("/Script/Engine.Material'/Game/Material/nowPlayerMaterial.nowPlayerMaterial'"));
    triangleStaticMaterial = TrianMaterial.Object;
}
void ATiaoQi::initColor(const FLinearColor& TargetColor) {
    DynMaterial = UMaterialInstanceDynamic::Create(triangleStaticMaterial, this);
    //也可以在这声明一个局部变量作为DynMaterial
    DynMaterial->SetVectorParameterValue(FName(TEXT("Color")), TargetColor); //修改动态材质参数
    playerTriangle->SetMaterial(0, DynMaterial);
}

蓝图调用C++函数

在头文件中给需要调用的函数声明前添加UFUNCTION(BlueprintCallable)即可。

编译后,在蓝图中,获得需要的对象,然后Cast to对应类,再调用函数即可。

蓝图访问C++成员

在头文件中给需要访问的变量声明前添加UPROPERTY(BlueprintReadWrite)即可。

编译后,在蓝图中,获得需要的对象,然后Cast to对应类,即可读取和修改其成员。

C++调用用户控件蓝图函数

思路是这样的,用户控件蓝图继承某个C++控件类,并用蓝图重写其部分C++函数。在控件蓝图中调用指定C++类的函数,使其获得蓝图对象的指针。随后在该C++类中即可直接使用该指针调用C++控件类的函数,即调用了用户控件蓝图的函数。

为什么不直接把用户控件定为C++类而要蓝图呢?因为C++类不提供UI界面的设计器,要直接敲代码,简直没法用。

TiaoQiWidget.h:C++ UserWidget类,控件蓝图继承该类。

UCLASS()
class UNREALTIAOQI_API UTiaoQiWidget : public UUserWidget
{
    GENERATED_BODY()
public:
    UFUNCTION(BlueprintNativeEvent)
    void humanFinish();  //这是需要调用的函数,在蓝图中重写它。
    virtual void humanFinish_Implementation() {}; //必须按照这种形式声明,即在原来的函数后面添加_Implementation,不可声明其=0,但可以为空。
};

TiaoQi.h:

class UTiaoQiWidget;
UCLASS()
class UNREALTIAOQI_API ATiaoQi : public APawn
{
    GENERATED_BODY()
public:
    UTiaoQiWidget* widget;
    UFUNCTION(BlueprintCallable)
    void setWidget(UTiaoQiWidget* w=nullptr);
}

TiaoQi.cpp:

#include "TiaoQiWidget.h"
void ATiaoQi::setWidget(UTiaoQiWidget* w) { //在控件蓝图中调用,参数为self即可。
    widget = w; //之后即可调用widget->humanFinish();
}

播放wav格式音效

.h

UCLASS()
class UNREALTIAOQI_API ATiaoQi : public APawn
{
    GENERATED_BODY()
public:
    USoundWave* jumpSoundWave[6];
    ATiaoQi();
    void playSound();
}

.cpp

#include "Sound/SoundWave.h"
const FString jumpSound[5] = { 
TEXT("/Script/Engine.SoundWave'/Game/soundFX/Jump/s1.s1'"), 
TEXT("/Script/Engine.SoundWave'/Game/soundFX/Jump/s2.s2'"), 
TEXT("/Script/Engine.SoundWave'/Game/soundFX/Jump/s3.s3'"),						 
TEXT("/Script/Engine.SoundWave'/Game/soundFX/Jump/s4.s4'"),
TEXT("/Script/Engine.SoundWave'/Game/soundFX/Jump/s5.s5'") };

const FString nextStepSound[5] = {
TEXT("/Script/Engine.SoundWave'/Game/soundFX/humanNextStep/button01_mp3.button01_mp3'"),
TEXT("/Script/Engine.SoundWave'/Game/soundFX/humanNextStep/button03_mp3.button03_mp3'"),
TEXT("/Script/Engine.SoundWave'/Game/soundFX/humanNextStep/button04_mp3.button04_mp3'"),
TEXT("/Script/Engine.SoundWave'/Game/soundFX/humanNextStep/button05_mp3.button05_mp3'"),
TEXT("/Script/Engine.SoundWave'/Game/soundFX/humanNextStep/button06_mp3.button06_mp3'") };

ATiaoQi::ATiaoQi() {
    for (int i = 0; i < 5; ++i) {
        ConstructorHelpers::FObjectFinder<USoundWave> jumpSoundObject(*jumpSound[i]);
        jumpSoundWave[i] = jumpSoundObject.Object;
    }
}
void ATiaoQi::playSound(){
    const FVector3d T = ...; // 发声地点,省略具体值。
    UGameplayStatics::PlaySoundAtLocation(this, jumpSoundWave[FMath::RandRange(0, 4)], T);
    //下面这种动态载入方式也可以
    USoundWave* HitSound = LoadObject<USoundWave>(this, *nextStepSound[FMath::RandRange(0,4)]); 
    //下面这种是直接发声,无关具体位置。
    if (HitSound) {UGameplayStatics::PlaySound2D(GetWorld(), HitSound);}
}

热门相关:冉冉心动   最强反套路系统   重生当学神,又又又考第一了!   朕是红颜祸水   朕是红颜祸水