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

前提

有必要说一下,静态这块内容加载时我用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"));
	}
}

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值