UE5 动态加载资源和类

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	if (MyActor)
	{
		UE_LOG(LogTemp,Warning,TEXT("MyActor is %s"),*MyActor->GetName());
	}
	//动态加载资源
	UStaticMesh* MyTmpStaticMesh = LoadObject(nullptr,TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Pipe_180.Shape_Pipe_180'"));
	if (MyTmpStaticMesh)
	{
		MyMesh->SetStaticMesh(MyTmpStaticMesh);
	}
	//动态加载类资源
	UClass* MyTmpClass = LoadClass(this, TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'"));
	if (MyTmpClass)
	{
		AActor* SpawnActor = GetWorld()->SpawnActor(MyTmpClass,FVector::ZeroVector,FRotator::ZeroRotator);
	}
	
}

因为是动态加载,所以不用在构造的时候去加载。这里再BeginPlay里加载。

加载StaticMesh等资源,就使用LoadObject(nullptr,TEXT("Copy Reference"))

加载类资源,比如蓝图Actor类

就使用LoadClass(this,TEXT("Copy Reference"))

但是同样要在最后一个字母后+_C

 TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'")

SetStaticMesh测试动态加载的模型是否能赋值

GetWorld()->SpawnActor(UClass*,FVector::ZeroVector,FRotator::ZeroRotator);

使用生成物体的方式,测试类。

你可能感兴趣的:(ue5,c++,学习,笔记)