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);

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

相关推荐
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
西岸行者3 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
哇哈哈20213 天前
信号量和信号
linux·c++
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
starlaky4 天前
Django入门笔记
笔记·django
勇气要爆发4 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
悠哉悠哉愿意4 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode