UE5 C++(九)— 静态、动态加载类和资源

2023-12-26 13:44:22

前提

有必要说一下,静态这块内容加载时我用UE5.2版本出现调用静态资源不是显示问题,修改后容易崩。所以,这里不建议5.2版本,直接用5.3,就不会出现这个问题。

导入初学者资源包
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

静态加载类和资源

静态加载资源

复制静态资源引用
在这里插入图片描述
/Script/Engine.StaticMesh’/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone’

#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 "MyCustomActor.generated.h"

UCLASS()
class EXAMPLE_API AMyCustomActor : public AActor
{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AMyCustomActor();

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 = "MyScenet")
	USceneComponent *MySceneComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")
	UStaticMeshComponent *MyMeshComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScenet")
	UBoxComponent *MyBoxComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")
	UAudioComponent *MyAudioComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")
	UParticleSystemComponent *MyParticleSystemComponent;
};
#include "MyCustomActor.h"

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

	// 创建组件
	MySceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("CustomScene"));
	MyMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));
	MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CustomBox"));
	MyAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CustomAudio"));
	MyParticleSystemComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("CustomParticleSystem"));

	// 把组件添加到根组件
	RootComponent = MySceneComponent;
	MyMeshComponent->SetupAttachment(MySceneComponent);
	MyBoxComponent->SetupAttachment(MySceneComponent);
	MyAudioComponent->SetupAttachment(MyBoxComponent);
	MyParticleSystemComponent->SetupAttachment(MySceneComponent);

	// 静态加载资源
	// 静态Mesh
	static ConstructorHelpers::FObjectFinder<UStaticMesh> TempStaticMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone'"));
	MyMeshComponent->SetStaticMesh(TempStaticMesh.Object);
	// 粒子特效
	static ConstructorHelpers::FObjectFinder<UParticleSystem> TempParticleSystem(TEXT("/Script/Engine.ParticleSystem'/Game/StarterContent/Particles/P_Explosion.P_Explosion'"));
	MyParticleSystemComponent->SetTemplate(TempParticleSystem.Object);
	// 音频
	static ConstructorHelpers::FObjectFinder<USoundWave> TempSoundBase(TEXT("/Script/Engine.SoundWave'/Game/StarterContent/Audio/Explosion02.Explosion02'"));
	MyAudioComponent->SetSound(TempSoundBase.Object);
}

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

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

编译后,会显示引用上
在这里插入图片描述
然后想,创建蓝图类BP_MyCustomActor

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
到这里,都显示引用上。

静态加载类

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyClasss")
	TSubclassOf<AActor> MyActorClass;

加载静态类时,在引用资源类的末尾需要加上 _C,否则编译时会报错

	// 静态加载类  加载静态类时,在引用资源类的末尾需要加上 _C,否则编译时会报错
	static ConstructorHelpers::FClassFinder<AActor> TempActorClass(TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight_C'"));
	MyActorClass = TempActorClass.Class;
	UE_LOG(LogTemp, Warning, TEXT("MyActorClass is %s"), *MyActorClass->GetName());

编译后,创建蓝图,打开后打印日志
在这里插入图片描述

动态加载类和资源

动态资源

void AMyCustomActor::BeginPlay()
{
	Super::BeginPlay();
	// 动态加载资源
	UStaticMesh *TempStaticMesh = LoadObject<UStaticMesh>(nullptr, TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_NarrowCapsule.Shape_NarrowCapsule'"));

	if (TempStaticMesh)
	{
		MyMeshComponent->SetStaticMesh(TempStaticMesh);
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("TempStaticMesh is null"));
	}
}

编译后,创建蓝图拖到场景中,点击运行
在这里插入图片描述

动态加载类

void AMyCustomActor::BeginPlay()
{
	// 动态加载类
	UClass *TempActorClass = LoadObject<UClass>(this, TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'"));
	if (TempActorClass)
	{
		AActor *TempActor = GetWorld()->SpawnActor<AActor>(TempActorClass,FVector(0,0,0),FRotator(0,0,0));
		UE_LOG(LogTemp, Warning, TEXT("TempActorClass is %s"), *TempActorClass->GetName());
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("TempActorClass is null"));
	}
}

编译后,创建蓝图拖到场景中,点击运行
在这里插入图片描述

文章来源:https://blog.csdn.net/qq_40120946/article/details/135127175
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。