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

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

相关推荐
小陈phd8 分钟前
系统架构师学习笔记(二)——计算机体系结构之指令系统
笔记·学习·系统架构
努力中的编程者14 分钟前
二叉树(C语言底层实现)
c语言·开发语言·数据结构·c++·算法
qq_4160187229 分钟前
高性能密码学库
开发语言·c++·算法
吃杠碰小鸡32 分钟前
前端 IndexedDB 完全指南
学习
宵时待雨40 分钟前
C++笔记归纳14:AVL树
开发语言·数据结构·c++·笔记·算法
呜喵王阿尔萨斯1 小时前
C and C++ code
c语言·开发语言·c++
问道飞鱼1 小时前
【大模型学习】LangGraph 深度解析:定义、功能、原理与实践
数据库·学习·大模型·工作流
旺仔.2911 小时前
顺序容器:deque双端队列 详解
c++
左左右右左右摇晃1 小时前
JDK 1.7 ConcurrentHashMap——分段锁
java·开发语言·笔记
烤麻辣烫1 小时前
I/O流 基础流
java·开发语言·学习·intellij-idea