动态加载类和资源:指在运行时加载
.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:指这段代码自身,相当于蓝图的selfif (MyTempClass){//就是SpawnActor蓝图节点GetWorld()->SpawnActor<AActor>(MyTempClass, FVector::ZeroVector, FRotator::ZeroRotator); //缩放值不写默认为1。}
}
运行后: