UE5 C++ 静态加载资源和类

时间:2024-02-22 22:05:18

一.上篇文章创建组件并绑定之后

在Actor中加载初始化了组件,现在在组件中赋值。使用static ConstructorHelpers::FObjectFinder<T>TempName(TEXT("Copy Reference"));再用TempName.Object


	//静态加载资源
	static ConstructorHelpers::FObjectFinder<UStaticMesh>TempStaticMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Torus.Shape_Torus'"));
	MyMesh->SetStaticMesh(TempStaticMesh.Object); //设置模型
	static ConstructorHelpers::FObjectFinder<UParticleSystem>TempParticleSystem(TEXT("/Script/Engine.ParticleSystem'/Game/StarterContent/Particles/P_Steam_Lit.P_Steam_Lit'")); //Copy Reference
	MyParticle->SetTemplate(TempParticleSystem.Object);
	static ConstructorHelpers::FObjectFinder<USoundWave>TempSound(TEXT("/Script/Engine.SoundWave'/Game/StarterContent/Audio/Explosion02.Explosion02'"));
	MyAudio->SetSound(TempSound.Object);

里面的资源都来自StarterContent

 

效果如下:

二.静态加载类

1.在Actor中再声明一个AActor类 

2.在静态加载类时使用

static ConstructorHelpers::FClassFinder<AActor>TempName(TEXT("Copy Reference_C"))

_C需要在 Copy Reference中自带的 ' 内(最后一个字母后面)

再赋值TempName.Class

	//静态加载类
	static ConstructorHelpers::FClassFinder<AActor>TempMyActor(TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight_C'")); //资源类一定要加_C
	MyActor = TempMyActor.Class;

测试一下