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

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

相关推荐
优橙教育3 分钟前
通信行业四大热门岗位解析:谁才是数字时代的黄金赛道?
网络·学习·5g
西西学代码26 分钟前
A---(1)
学习
厦门小杨28 分钟前
数据驱动制造:智能铺布机如何成为服装工厂数字化的基石
学习·制造·服装厂·服装机械
一只小小的芙厨33 分钟前
AT_tkppc3_d 巨大チェスボード 题解
c++·题解
QT.qtqtqtqtqt34 分钟前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
我在人间贩卖青春36 分钟前
C++之继承与派生类的关系
c++·向上造型·向下造型
Trouvaille ~37 分钟前
【Linux】应用层协议设计实战(二):Jsoncpp序列化与完整实现
linux·运维·服务器·网络·c++·json·应用层
DeanWinchester_mh41 分钟前
DeepSeek新论文火了:不用卷算力,一个数学约束让大模型更聪明
人工智能·学习
EmbedLinX1 小时前
嵌入式之协议解析
linux·网络·c++·笔记·学习
楚轩努力变强1 小时前
iOS 自动化环境配置指南 (Appium + WebDriverAgent)
javascript·学习·macos·ios·appium·自动化