FloatingActor.cpp

我们一键生成了FloatingActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "FloatingActor.h"

// Sets default values
AFloatingActor::AFloatingActor()
{
 	// 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;

}

// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}


AFloatingActor : 基于 继承自 AActor 的类前缀为A。的命名规范

FloatingActor.h

我们一键生成了FloatingActor.h
此为 标头 文件。可以将它视作C++类的目录之类的东西。 要开始编译任何新功能,必须首先声明在此文件中使用的所有新 变量 或 函数。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"

UCLASS()
class UE5BUILD_API AFloatingActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AFloatingActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

在 AFloatingActor() 的声明下面添加下列代码:

UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* VisualMesh;

这里声明的是 StaticMeshComponent,它将担当对象的视觉表示。请注意,它使用 UProperty 宏,这使它在虚幻编辑器中可见。 欲了解UProperty及其说明符的更多信息,请参见属性页面。

https://docs.unrealengine.com/5.0/zh-CN/unreal-engine-uproperties/
VisibleAnywhere 说明此属性在所有属性窗口中可见,但无法被编辑。此说明符与"Edit"说明符不兼容。

现在打开 FloatingActor.cpp,在 AFloatingActor::AFloatingActor() 中将下列代码添加到右大括号前:

VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);

static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

if (CubeVisualAsset.Succeeded())
{
    VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
    VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}

此函数是 构造函数,它在类首次创建时告诉该类如何初始化自身。我们添加的代码将会在VisualMesh引用中填充新的StaticMeshComponent、 将它附加到Actor,并将它设为 初学者内容包 资源中的立方体模型。欲了解在代码中附加组件的更多信息,请参见创建和附加组件的指南。

在 AFloatingActor::Tick(float DeltaTime) 中将下列代码添加到右大括号前:

FVector NewLocation = GetActorLocation();
FRotator NewRotation = GetActorRotation();
float RunningTime = GetGameTimeSinceCreation();
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
float DeltaRotation = DeltaTime * 20.0f;    //Rotate by 20 degrees per second
NewRotation.Yaw += DeltaRotation;
SetActorLocationAndRotation(NewLocation, NewRotation);

我们在 Tick 函数中添加要实时执行的代码。在此例中,它将使立方体在上下浮动的同时旋转。 欲了解对Actor进行Tick的更多信息,请参见对Actor进行Tick。

编译和测试C++代码

保存 在 FloatingActor.h 以及 FloatingActor.cpp 中的工作成果。然后在 解决方案浏览器 中右键点击项目,点击快捷菜单中的 编译(Build) 命令,然后等待项目完成编译。

应会在窗口底部的 输出 日志中看到一条显示"已成功(Succeeded)"的消息。

或者,可以回到 虚幻编辑器,点击屏幕顶部工具栏中的 编译(Compile) 按钮。

在 虚幻编辑器 中,转到 内容浏览器,展开 C++类(C++ Classes),然后找到 FloatingActor。它将位于与项目同名的文件夹中,在范例中是QuickStart。

点击 FloatingActor 并将它拖进 透视视口 来创建FloatingActor的实例。它将在 世界大纲视图 中作为"FloatingActor1"处于被选中状态,它的属性将显示在 细节(Details) 面板中。

如何删除C++代码

无法通过虚幻编辑器直接删除C++代码构建的类,
要删除,需要先关闭虚幻编辑器,然后去根目录删除Binaries文件夹。

更多推荐