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

同一Reality Composer场景多副本如何设置不同onAction回调?

问题原因与解决方案

核心原因

  1. 锚点类型限制手动修改位置
    若你的Reality Composer场景选择了「Horizontal Plane」或「Vertical Plane」这类平面跟踪锚点,加载后会生成ARAnchorEntity——这类锚点的位置由AR会话自动管理,手动设置.position会被系统覆盖,导致你尝试移动整个场景的操作无效。

  2. 子实体移动导致碰撞体空间重叠
    你仅修改了helloScene1子实体的位置,但两个场景的锚点仍处于同一空间位置(因锚点位置无法手动修改),使得两个可点击盒子的碰撞体重叠:顶部盒子在锚点上方,底部盒子在锚点初始位置。此时点击顶部盒子时,射线检测会同时命中两个重叠的碰撞体,触发两个回调;点击底部盒子时则被顶部碰撞体遮挡,无响应。

解决方案

1. 修改场景锚点类型

打开Reality Composer,将场景锚点类型改为「None」或「Fixed」,加载后得到普通AnchorEntity,即可手动设置位置移动整个场景。

2. 调整代码:移动锚点而非子实体

修改代码直接设置克隆后锚点的位置,避免子实体移动导致的碰撞体重叠:

func loadScene() {
    guard let sceneTemplate = try? HelloComposer.loadMainScene() 
    else { return }

    let helloScene1 = sceneTemplate.clone(recursive: true)
    let helloScene2 = sceneTemplate.clone(recursive: true)

    // 直接设置锚点位置,替代修改子实体位置
    helloScene1.position = simd_make_float3(0, 0.5, 0)
    // 可按需设置helloScene2的位置,避免重叠
    // helloScene2.position = simd_make_float3(0, -0.5, 0)

    helloScene1.actions.tapTheBox.onAction = { entity in
        print("top box tapped")
    }

    helloScene2.actions.tapTheBox.onAction = { entity in
        print("bottom box tapped")
    }

    arView.scene.anchors.append(helloScene1)
    arView.scene.anchors.append(helloScene2)
}

3. 可选:验证触发实体

可在回调中打印实体标识,确认触发来源:

helloScene1.actions.tapTheBox.onAction = { entity in
    guard let entity = entity else { return }
    print("top box tapped, entity: \(entity.name ?? "unknown")")
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:25:21