动态加载类和资源:指在运行时加载
.cpp
void AMyActor::BeginPlay()
{
Super::BeginPlay();
//动态加载资源
UStaticMesh* MyTempStaticMesh = LoadObject<UStaticMesh>(nullptr,TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_NarrowCapsule.Shape_NarrowCapsule'")); //nullptr:空指针
if (MyTempStaticMesh)
{
MyMesh->SetStaticMesh(MyTempStaticMesh);
}
//动态加载类
UClass* MyTempClass = LoadClass<AActor>(this,TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'")); //AActor:Actor类型;this:指这段代码自身,相当于蓝图的self
if (MyTempClass)
{
//就是SpawnActor蓝图节点
GetWorld()->SpawnActor<AActor>(MyTempClass, FVector::ZeroVector, FRotator::ZeroRotator); //缩放值不写默认为1。
}
}
运行后: