You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unreal Engine运行时动态创建SphereComponent不显示如何解决

Unreal Engine 运行时动态创建可见球体组件问题

我需要在关卡启动2秒后动态创建一个SphereComponent,为此编写了如下代码:

void ACollidingPawnSpawnPawns::DelayedFunction() {
         // 第一个参数SphereComponent是关卡主组件,已在当前类构造函数中通过CreateDefaultSubobject方法创建,但该方法无法在构造函数外调用
         USphereComponent *dynamicallyCreatedSphere = NewObject<USphereComponent>(SphereComponent, USphereComponent::StaticClass());
             dynamicallyCreatedSphere ->InitSphereRadius(30.0f);
             dynamicallyCreatedSphere ->SetCollisionProfileName(TEXT("Pawn"));
             dynamicallyCreatedSphere ->SetRelativeLocation(FVector(155.0f, 165.0f, 45.0f));
             dynamicallyCreatedSphere ->SetVisibility(true);
 }

运行关卡后我没有看到这个动态球体出现。
可正常显示的主SphereComponent在构造函数中的实现代码如下:

// 根组件设置为可响应物理的球体
 SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
 RootComponent = SphereComponent;
 SphereComponent->InitSphereRadius(40.0f);
 SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
 
    // 创建并放置网格体组件,用于显示球体位置
     UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
     SphereVisual->SetupAttachment(RootComponent);
     static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
     if (SphereVisualAsset.Succeeded())
     {
         SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
         SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
         SphereVisual->SetWorldScale3D(FVector(0.8f));
     }

但我无法在构造函数外使用CreateDefaultSubobject创建UStaticMeshComponent。
我推测要让动态创建的dynamicallyCreatedSphere在关卡中可见,需要创建UStaticMeshComponent,同时需要调用FObjectFinder的替代方法(FObjectFinder同样无法在构造函数外使用)。
这个问题我已经卡了很久,有人知道该如何实现吗?


2021-12-2 更新

以下是最终实现动态创建球体的代码:

cpp文件代码

void ACollidingPawnSpawnPawns::spawnPawns()
{
    if (dynamicallyCreatedSphere == nullptr) {
        //dynamicallyCreatedSphere = NewObject<USphereComponent>(USphereComponent::StaticClass()); // 可正常编译
        dynamicallyCreatedSphere = NewObject<USphereComponent>(SphereComponent, USphereComponent::StaticClass());
        //dynamicallyCreatedSphere->SetupAttachment(SphereComponent);
        dynamicallyCreatedSphere->InitSphereRadius(30.0f);
        dynamicallyCreatedSphere->SetCollisionProfileName(TEXT("Pawn"));
        dynamicallyCreatedSphere->SetRelativeLocation(FVector(155.0f, 165.0f, 45.0f));
        dynamicallyCreatedSphere->SetVisibility(true);


        dynamicMesh = NewObject<UStaticMeshComponent>(this);
        dynamicMesh->AttachToComponent(dynamicallyCreatedSphere, FAttachmentTransformRules::KeepWorldTransform);
        dynamicMesh->RegisterComponent();
        dynamicMesh->SetStaticMesh(StaticMesh);
    }
}

.h文件成员定义

UPROPERTY(EditAnywhere)
UStaticMesh* StaticMesh;

USphereComponent* dynamicallyCreatedSphere = nullptr;
UStaticMeshComponent* dynamicMesh = nullptr;

将StaticMesh声明为UProperty后,可在UE编辑器界面中手动选择网格体,运行关卡时该StaticMesh就会被赋值到dynamicMesh变量中。


内容的提问来源于stack exchange,提问作者CAMD_3441

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 03:36:03