1. 普通类 UObject
1.1 声明
UCLASS()
class MYPROJECT_API UMyObject : public UObject
{
GENERATED_BODY()
public:
UPROPERTY()
FMyDateTableStruct myDataTable;
};
1.2 示例化
UCLASS()
class MYPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
UPROPERTY()
UMyObject* m_pMyObj;
};
TSubclassOf<UMyObject> scMyObj = UMyObject::StaticClass();
this->m_pMyObj = NewObject<UMyObject>(GetWorld(), scMyObj);
2. 单例类 UGameInstance
1.1 用法
1. 持久性: GameInstance
是在游戏启动时创建的,并且在整个游戏会话中一直存在,直到游戏关闭。它不会像关卡(Levels)或游戏模式(GameModes)那样在加载新关卡时重置。
2. 全局数据管理: GameInstance
通常用于存储整个游戏会话中需要持续访问的数据,如玩家偏好设置、累积的分数、游戏进度等。
3. 跨关卡数据传递: 由于 GameInstance
在加载新关卡时不会重置,因此它非常适合用于在多个关卡之间传递数据和状态。
4. 网络游戏中的作用: 在网络游戏中,GameInstance
可以用于管理和维护网络会话,例如创建、加入、管理多人游戏会话。
5. 访问全局服务: GameInstance
还常被用作访问全局服务(如音频管理、网络服务)的入口点。
6. 自定义扩展: 开发者可以继承 GameInstance
类来实现自己的逻辑,以支持特定的游戏需求。这使得 GameInstance
成为一个强大的工具,可用于处理游戏中的各种全局任务和功能。
1.2 声明
UCLASS()
class MYPROJECT_API UMyGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UMyGameInstance();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyInstance")
FString m_sMyAppId;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyInstance")
FString m_sMyUserId;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyInstance")
FString m_sMyName;
};
1.3 使用
UCLASS()
class MYPROJECT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
UPROPERTY()
UMyGameInstance* m_pGameInstance;
};
打印
this->m_pGameInstance = Cast<UMyGameInstance>(GetWorld()->GetFirstPlayerController()->GetGameInstance());
if (this->m_pGameInstance)
{
UE_LOG(LogTemp, Warning, TEXT("GameInstance is %s. "), *this->m_pGameInstance->GetName());
UE_LOG(LogTemp, Warning, TEXT("AppId is %s. "), *this->m_pGameInstance->m_sMyAppId);
UE_LOG(LogTemp, Warning, TEXT("UserId is %s. "), *this->m_pGameInstance->m_sMyUserId);
UE_LOG(LogTemp, Warning, TEXT("MyName is %s. "), *this->m_pGameInstance->m_sMyName);
}
3. AActor
3.1 MyActor.h
#pragma once
#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "Components/AudioComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class MYPROJECT_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;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
class USceneComponent* m_pMyScene;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyStaticMeshComponent")
class UStaticMeshComponent* m_pMyStaticMesh;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyBoxComponent")
class UBoxComponent* m_pMyBox;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyAudioComponent")
class UAudioComponent* m_pMyAudio;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyParticleSystemComponent")
class UParticleSystemComponent* m_pMyParticleSystem;
};
Tips:
- 变量前必须加
class
; - 变量与宏必须有一个缩进。
3.2 MyActor.cpp
#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;
// create
this->m_pMyScene = CreateDefaultSubobject<USceneComponent>(TEXT("MyCustomScene"));
this->m_pMyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyCustomStaticMesh"));
this->m_pMyBox = CreateDefaultSubobject<UBoxComponent>(TEXT("MyCustomBox"));
this->m_pMyAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("MyCustomAudio"));
this->m_pMyParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyCustomParticleSystem"));
// set level
RootComponent = this->m_pMyScene;
this->m_pMyStaticMesh->SetupAttachment(this->m_pMyScene);
this->m_pMyBox->SetupAttachment(this->m_pMyScene);
this->m_pMyAudio->SetupAttachment(this->m_pMyBox);
this->m_pMyParticleSystem->SetupAttachment(this->m_pMyScene);
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
3.3 结果
4. 静态加载类和资源
编译时加载,写在构造函数内:
// Sets default values
AMyActor::AMyActor()
{
// ****************************************************************************
// static loading
// ****************************************************************************
// load static mesh
static ConstructorHelpers::FObjectFinder<UStaticMesh> tempStaticMesh(
TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone'"));
// set mesh asset
this->m_pMyStaticMesh->SetStaticMesh(tempStaticMesh.Object);
}
4.1 Mesh Assert
// load static mesh
static ConstructorHelpers::FObjectFinder<UStaticMesh> tempStaticMesh(
TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone'"));
// set mesh asset
this->m_pMyStaticMesh->SetStaticMesh(tempStaticMesh.Object);
4.2 粒子
// load particle
static ConstructorHelpers::FObjectFinder<UParticleSystem> tempParticleSystem(
TEXT("/Script/Engine.ParticleSystem'/Game/StarterContent/Particles/P_Explosion.P_Explosion'"));
// set mesh asset
this->m_pMyParticleSystem->SetTemplate(tempParticleSystem.Object);
4.3 AActor
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyClass")
TSubclassOf<AActor> m_pMyLightActor;
使用 FClassFinder
,该处示例为资源包中一个灯的类。
static ConstructorHelpers::FClassFinder<AActor> tempLightActor(
TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight'"));
this->m_pMyLightActor = tempLightActor.Class;
5. 动态加载类和资源
运行时加载,可写在 BeginPlay()
里或其他:
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
// load asset
// ...
}
5.1 Mesh Assert
这里的模型是一个正方体
UStaticMesh* pStaticMeshAsset = LoadObject<UStaticMesh>(nullptr,
TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube'"));
if (pStaticMeshAsset)
{
this->m_pMyStaticMesh->SetStaticMesh(pStaticMeshAsset);
}
5.2 AActor
UClass* pClassAsset = LoadClass<UClass>(nullptr,
TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight'"));
if (pClassAsset)
{
// (mesh, pos, rot)
AActor* spawnActor = GetWorld()->SpawnActor<AActor>(pClassAsset, FVector::ZeroVector, FRotator::ZeroRotator);
}
5.3 结果展示
放置在场景中,未运行时,展示的是静态加载的圆锥Mesh:
运行时,该对象展示的变成了动态加载的立方体Mesh: