UE5 动态加载资源和类

cpp 复制代码
// 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<UStaticMesh>(nullptr,TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Pipe_180.Shape_Pipe_180'"));
	if (MyTmpStaticMesh)
	{
		MyMesh->SetStaticMesh(MyTmpStaticMesh);
	}
	//动态加载类资源
	UClass* MyTmpClass = LoadClass<AActor>(this, TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'"));
	if (MyTmpClass)
	{
		AActor* SpawnActor = GetWorld()->SpawnActor<AActor>(MyTmpClass,FVector::ZeroVector,FRotator::ZeroRotator);
	}
	
}

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

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

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

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

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

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

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

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

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

相关推荐
特种加菲猫18 分钟前
继承,一场跨越时空的对话
开发语言·c++
WBluuue32 分钟前
Codeforces 1093 Div2(ABCD1D2)
c++·算法
玩转单片机与嵌入式1 小时前
玩转边缘AI(TInyML):需要掌握的C++知识汇总!
开发语言·c++·人工智能
历程里程碑2 小时前
4 Git远程协作:从零开始,玩转仓库关联与代码同步(带实操代码讲解)
大数据·c++·git·elasticsearch·搜索引擎·gitee·github
汉克老师2 小时前
GESP5级C++考试语法知识(贪心算法(一)课堂例题精讲)
c++·贪心算法·gesp5级·gesp五级·贪心规律
墨染千千秋2 小时前
C++头文件的使用,和各个头文件与头文件用处
c++
呱呱巨基2 小时前
Linux 基础IO
linux·c++·笔记·学习
旖-旎2 小时前
深搜练习(N皇后)(10)
c++·算法·深度优先·力扣
头发够用的程序员3 小时前
C++和Python面试经典算法汇总(一)
开发语言·c++·python·算法·容器·面试
水龙吟啸4 小时前
数据结构与算法随机复习–Day1
数据结构·c++·算法