UE4(虚幻引擎)程序改变世界程序员

unreal C++获得 actor 以及component

2017-08-13  本文已影响293人  齊葩

1:获得场景里的actor

for (TActorIterator<AStaticMeshActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
    AStaticMeshActor *Mesh = *ActorItr;
    UE_LOG(LogTemp, Warning, TEXT("%s"), *(ActorItr->GetName()));
}

2:获得场景里直接拖到actor上面的actor

TArray<AActor*>aa;
actor->GetAttachedActors(aa);
for (int32 b = 0; b < aa.Num(); b++)
{
    AActor*a = aa[b];  
    UE_LOG(LogTemp, Warning, TEXT("%s"), *(a->GetName()));
}

3:获得actor上面的component 通过类型

TArray<UActorComponent*>aa; //这里填什么类型获得的就是什么类型的 component
actor->GetComponents(aa);
for (int32 b = 0; b < aa.Num(); b++)
{
    UActorComponent*a = aa[b];
    UE_LOG(LogTemp, Warning, TEXT("%s"), *(a->GetName()));
}

4:获得actor上面的component 通过class

actor->GetComponentByClass(UActorComponent::StaticClass());//填你想获得的componet 的类

5:获得actor上面的component 通过class(所有这个类型的component)比上面的方法多了一个s

TArray<UActorComponent*> ll( actor->GetComponentsByClass(UActorComponent::StaticClass()));//填你想获得的componet 的类
for (int32 b = 0; b < ll.Num(); b++)
{
    UActorComponent*a = ll[b];
    UE_LOG(LogTemp, Warning, TEXT("%s"), *(a->GetName()));
}

6:获得actor上面的root component 下面的所有component //不常用

TArray<USceneComponent*> My_Children;
actor->GetRootComponent()->GetChildrenComponents(false, My_Children);
for (int32 b = 0; b < My_Children.Num(); b++)
{
    USceneComponent*a = My_Children[b];
    UE_LOG(LogTemp, Warning, TEXT("%s"), *(a->GetName()));
}

7:获得actor上面的UChildComponent 上面的actor //不常用

TArray<AActor*>aa;
actor->GetAllChildActors(aa,true);
for (int32 b = 0; b < aa.Num(); b++)
{
    AActor*a = aa[b];
    UE_LOG(LogTemp, Warning, TEXT("%s"), *(a->GetName()));
}

8:AActor 类里 有一个Children 属性,这个是服务器用来复制网络消息的,客户端一般用不到

/** Array of Actors whose Owner is this actor */
    UPROPERTY(transient)
    TArray<AActor*> Children;

************点击这里可以看到作者的其他文章************
********欢迎转载,转载请标明出处: http://www.jianshu.com/p/48e53622d8cc ********

上一篇下一篇

猜你喜欢

热点阅读