虚幻C++概念
虚幻C++类的继承结构
- 虚幻引擎C++类层级结构(Hierarchy)

- 这些基本类又派生出了很多子类,例:

UE中的反射与垃圾回收系统
- 例如一个创建了一个Actor类,有一个Actor类型指针去指向这个Actor类,如果的指针被销毁了,那么这个Actor类UE系统会判断为垃圾,参与了反射与垃圾回收系统后,UE会在适当的时机销毁这个Actor
- 使用宏进行标识,UE的UHT系统会帮我们进行垃圾回收

创建第一个UObject子类
- 新建一个基于Object的子类


- 这里是灰色的,是因为没有指定反射角色

- 我们可以将这个类指定一下给蓝图


创建UObject的蓝图类与基础宏参数
- 将MyObject创建类图类


- 因为是最开始的基类,不能显示,所以没有可视化的脚本编辑框

- 设置变量与函数到蓝图中


- 运行结果,写的数据就可以到UE的蓝图类中进行使用

使用UE_LOG打印日志与在蓝图中实例化继承于Object的类
- UPROPERTY(BlueprintReadWrite,
Category= “My Variables”),中的Category是分类的意思,可以在蓝图调用的时候去显现出来会将你的东西挂载到这个分类下面。 - MyObject.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyObject.generated.h"
/**
*
*/
UCLASS(Blueprintable)
class CPROJECT_API UMyObject : public UObject
{
GENERATED_BODY()
public:
//构造函数
UMyObject();
UPROPERTY(BlueprintReadWrite, Category = "My Variables")//声明变量可以蓝图系统中进行读写
//变量
float xiaogua;
UFUNCTION(BlueprintCallable, Category = "My Functions")//声明函数可以在蓝图中进行调用
//函数成员
void myFunction();
};
- MyObject.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyObject.h"
UMyObject::UMyObject()
{
xiaogua = 0.0f;
}
void UMyObject::myFunction()
{
//第一个变量是输出类型,第二个变量是输出级别,第三个输出内容
//LogTemp临时类型,Log,Warning,Error输出级别
UE_LOG(LogTemp,Log,TEXT("Hello,World"));
UE_LOG(LogTemp, Warning, TEXT("Hello,World"));
UE_LOG(LogTemp, Error, TEXT("Hello,World"));
}


- Object类是不能放入到场景中的,我们需要在UE的关卡蓝图中去实例化,使用Construct Object from Class蓝图专门用来实例化继承Object类的,还可以将实例化的对象提升出来

- 运行后,就可以看见输出内容了

删除自定义C++类
- 蓝图部分中直接删除保存即可
- 找到项目路径中的source下的项目名里面的自己写的类删除,其他类不要动

- 然后删除这个文件

- 最后重新生成一下这个项目文件

- 因为删除了一些文件,直接点是重建即可

Actor类与相关API
创建自己的Actor子类
UE中的前缀
- 派生自Actor的类带有
A前缀,如AController - 派生自Object的类带有
U前缀,如UComponent - Enums的前缀是
E,如EFortificationType - Interface的前缀是
I,如IAbilitySystemInterface - Template前缀是
T,如TArray - 派生自SWidget类的(Slate UI)带有前缀
S,如SButton - 其他类前缀为
F,如FVector - MyActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
- MyActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// 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 AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
- AActor基类有写入Blueprintable所以不需要在UCLASS里面写入,也可以直接创建蓝图实例化

组件简介与使用蓝图类扩展代码的优点
- 当创建好Actor默认类后生成蓝图,蓝图会自动给这个类设置个根组件进行辨识,可以添加自己的组件将其覆盖,只需要将组件拖拽到组件上,如果要回复默认组件,就将自己的组件删除即可

- 可以给予一些网格体与材质

- 就可以在世界里面显示

在C++中创建静态网格组件
UPROPERTY(VisibleAnyWhere,Category = " MyStatic")VisibleAnyWhere:设置可见属性为全部可见Category:种类
UStaticMeshComponent:创建一个静态网格体
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
- MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT(“MyStatic”));
- CreateDefaultSubobject:这是一个模版函数,返回创建的这个子对象

- CreateDefaultSubobject:这是一个模版函数,返回创建的这个子对象
- MyActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
- MyActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// 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;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
- 运行结果


控制Actor位置与宏参数介绍
EditInstanceOnly宏参数与Actor移动函数
- UPROPERTY(EditInstanceOnly, Category = “MyActorProperties | Vector”)
- EditInstanceOnly:只运行在实例上进行编辑
- Category = “MyActorProperties | Vector”:MyActorProperties 下的子文件夹Vector
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
- InitLocation = FVector(0.0f);:构造赋初值,构造方法很多,一般常用如下两种

- SetActorLocation:将Actor传送到新位置
- MyActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
- MyActor.pp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// 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;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
//构造赋初值
InitLocation = FVector(0.0f);
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
SetActorLocation(InitLocation);
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
- 运行结果,每次运行,Actor就会移动到默认(0,0,0)坐标位,因为设置了实例化编辑,可以在场景实例中更改FVector坐标位

VisibleInstanceOnly与EditDefaultsOnly
- UPROPERTY(VisibleInstanceOnly,Category = “MyActorProperties | Vector”)
- VisibleInstanceOnly:只运行在示例上进行显示
- Category = “MyActorProperties | Vector”:MyActorProperties 下的子文件夹Vector
UPROPERTY(VisibleInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量,用来记录上一次位置
FVector PlacedLocation;
- UPROPERTY(
EditDefaultsOnly,Category = “MyActorProperties | Vector”)- EditDefaultsOnly:只能在蓝图模版中进行编辑
- Category = “MyActorProperties | Vector”:MyActorProperties 下的子文件夹
- 虚幻中的bool变量,前必须加上b进行标识
UPROPERTY(EditDefaultsOnly,Category = "MyActorProperties | Vector")
//虚幻的bool变量前必须加上b,程序会默认去掉的,但是必须定义时必须要有
bool bGotoInitLocation;
- MyActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
UPROPERTY(VisibleInstanceOnly,Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量,用来记录上一次
FVector PlacedLocation;
UPROPERTY(EditDefaultsOnly,Category = "MyActorProperties | Vector")
//虚幻的bool变量前必须加上b,程序会默认去掉的,但是必须定义时必须要有
bool bGotoInitLocation;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
- MyActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
// Sets default values
AMyActor::AMyActor()
{
// 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;
//创建默认根组件,如果报错,因为设置了文本,报错会提示MyStatic错误
MyStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStatic"));
//构造赋初值
InitLocation = FVector(0.0f);
PlacedLocation = FVector(0.0f);
bGotoInitLocation = false;
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//获取位置
PlacedLocation = GetActorLocation();
if (bGotoInitLocation)
{
SetActorLocation(InitLocation);
}
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
- 运行结果,当这勾选时,场景中所有的此Actor都会设置到InitLocation位置


VisibleDefaultOnly与EditAnyWhere
- UPROPERTY(
VisibleDefaultsOnly, Category= “MyActorProperties | Vector”)- VisibleDefaultsOnly:只在蓝图模版中显示
- UPROPERTY(
EditAnywhere, Category = “MyActorProperties | Vector”)- EditAnywhere:设置编辑属性为全部能编辑
UPROPERTY(VisibleDefaultOnly,Category="MyActorProperties | Vector")
FVector WordOrigin;
UPROPERTY(EditAnywhere, Category = "MyActorProperties | Vector")
FVector LocationOffset;
- MyActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class CPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
//设置组件属性与种类
UPROPERTY(VisibleAnyWhere, Category = "MyStatic")
//定义一个静态组件
UStaticMeshComponent* MyStatic;
UPROPERTY(EditInstanceOnly, Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量
FVector InitLocation;
UPROPERTY(VisibleInstanceOnly,Category = "MyActorProperties | Vector")
//新建一个三维空间向量变量,用来记录上一次
FVector PlacedLocation;
UPROPERTY(EditDefaultsOnly

本文围绕虚幻C++开发展开,介绍了虚幻C++类的继承结构、反射与垃圾回收系统等概念,还阐述了Actor类、Pawn类相关API的使用,包括创建子类、添加组件、控制位置、绑定事件等内容,涵盖了从基础概念到实际操作的多方面知识。
最低0.47元/天 解锁文章
1158

被折叠的 条评论
为什么被折叠?



