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);
使用生成物体的方式,测试类。