You need to enable JavaScript to run this app. UE4如何用C++创建USceneComponent并支持在蓝图视口移动
优惠活动
大模型
产品
解决方案
定价
更多

UE4如何用C++创建USceneComponent并支持在蓝图视口移动

UE C++自定义场景组件可在子类蓝图视口拖拽移动的配置方案

头文件变量的UPROPERTY配置

要让C++定义的场景组件在子类蓝图视口可见且可编辑位置,需要给组件变量添加对应UPROPERTY标记,示例如下:

// 头文件.h中,以下配置适用于private权限的变量,public/protected可去掉AllowPrivateAccess元参数
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Custom Scene Components", meta = (AllowPrivateAccess = "true"))
USceneComponent* FirstCustomSceneComp;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Custom Scene Components", meta = (AllowPrivateAccess = "true"))
USceneComponent* SecondCustomSceneComp;

参数说明:

  • VisibleAnywhere:保证组件在编辑器属性面板、蓝图视口都可见,不可用VisibleDefaultsOnly(该参数仅允许在C++父类的默认设置中修改,子类蓝图无法编辑)
  • BlueprintReadOnly:蓝图中仅可读取组件属性,不可替换组件实例,若需要在蓝图中修改组件挂载关系可换成BlueprintReadWrite
  • Category:自定义属性分组,方便在编辑器中快速找到对应组件
  • meta=(AllowPrivateAccess = "true"):私有成员变量暴露给编辑器访问的必要配置,非私有变量可省略

构造函数创建与挂载的必要设置

仅添加UPROPERTY不足以让组件可移动,还需要在构造函数中完成创建、挂载、移动性设置,示例如下:

// .cpp文件的角色类构造函数中
AYourCharacterClass::AYourCharacterClass()
{
    // 创建第一个自定义场景组件
    FirstCustomSceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("FirstCustomSceneComp"));
    // 挂载到角色默认根组件(胶囊体),如果要将该组件设为新根,替换为SetRootComponent(FirstCustomSceneComp)
    FirstCustomSceneComp->SetupAttachment(GetRootComponent());
    // 设置组件移动性为可移动,是允许在视口拖拽位置的核心配置,Stationary/Static都无法直接移动
    FirstCustomSceneComp->SetMobility(EComponentMobility::Movable);

    // 创建第二个自定义场景组件
    SecondCustomSceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SecondCustomSceneComp"));
    SecondCustomSceneComp->SetupAttachment(GetRootComponent());
    SecondCustomSceneComp->SetMobility(EComponentMobility::Movable);
}

注意事项

  • 组件必须完成挂载操作,游离未挂载的组件不会在蓝图视口显示,也无法编辑
  • 如果需要在蓝图中修改组件的碰撞、渲染等更多属性,可将VisibleAnywhere替换为EditAnywhere
  • 若组件是挂载到其他自定义父组件下,需要保证父组件的移动性不低于子组件,比如子组件设为Movable时父组件不能是Static

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 15:24:04