如何列出ULevelStreaming中的所有参与者?

How to list all of the actors in ULevelStreaming?

本文关键字:参与者 何列出 ULevelStreaming      更新时间:2023-10-16

我找不到一种方法来列出子关卡(LevelStreaming(中的所有Actor。对于枚举,我尝试使用TActorIterator,但它的构造函数接受对UWorld的引用,它不接受对ULevelStreaming的引用,因为它们都继承自UObject,而ULevelStreaming不继承自UWorld。我可以像这样列出世界上所有的演员:

UWorld* world = GetWorld();
TActorIterator<AActor> Iterator(world, AActor::StaticClass());
for (; Iterator; ++Iterator)
{
AActor* Actor = Cast<AActor>(*Iterator);
GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Yellow, Actor->GetFName().ToString());  //Print name of the Actor
}

这有效,但我只从主地图(持久级别(中获取对象,而且我不知道如何从我添加为 ULevelStream 的其他级别中获取所有 Actor 的列表。 这是我的尝试(不起作用(:

UWorld* world = GetWorld();
auto levels = world->GetStreamingLevels();
for (auto level : levels) {
ULevelStreaming* currentLevel = Cast<ULevelStreaming>(level);
TActorIterator<AActor> It(currentLevel, AActor::StaticClass()); //Here is compiler error with TActorIterator's constructor
for (; It; ++It)
{
AActor* Actor = Cast<AActor>(*It);
GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Yellow, Actor->GetFName().ToString());
}
}

首先 - 必须加载级别。然后你可以使用GetLoadedLevel来获取ULevelULevel已经有了Actor- 所有 actor 的数组

UWorld* world = GetWorld();
auto StreamingLevels = world->GetStreamingLevels();
for (UStreamingLevel* StreamingLevel : StreamingLevels ) {
ULevel* level = StreamingLevel->GetLoadedLevel();
if(!level)
continue;
for (AActor* Actor : level->Actors)
{
// Actor
}
}