// 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());
}
//動態(tài)加載資源
UStaticMesh* MyTmpStaticMesh = LoadObject<UStaticMesh>(nullptr,TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Pipe_180.Shape_Pipe_180'"));
if (MyTmpStaticMesh)
{
MyMesh->SetStaticMesh(MyTmpStaticMesh);
}
//動態(tài)加載類資源
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);
}
}
因為是動態(tài)加載,所以不用在構(gòu)造的時候去加載。這里再BeginPlay里加載。
加載StaticMesh等資源,就使用LoadObject<UStaticMesh>(nullptr,TEXT("Copy Reference"))
加載類資源,比如藍(lán)圖Actor類
就使用LoadClass<AActor>(this,TEXT("Copy Reference"))
但是同樣要在最后一個字母后+_C
?TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'")
SetStaticMesh測試動態(tài)加載的模型是否能賦值
GetWorld()->SpawnActor<AActor>(UClass*,FVector::ZeroVector,FRotator::ZeroRotator);文章來源:http://www.zghlxwxcb.cn/news/detail-833050.html
使用生成物體的方式,測試類。文章來源地址http://www.zghlxwxcb.cn/news/detail-833050.html
到了這里,關(guān)于UE5 動態(tài)加載資源和類的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!