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

如何在SwiftUI中发送通知触发Reality Composer Pro时间线动画?

触发Reality Composer Pro时间线动作的实现方法

步骤1:在RCP中配置时间线动作的通知触发器

打开你的Reality Composer Pro项目,找到名为“SpinAndMove”的时间线动作:

  • 选中时间线,打开右侧的触发器面板
  • 添加一个通知触发器,设置通知名称为SpinAndMove(注意大小写需和后续代码完全一致)
  • 确保触发器绑定到对应的时间线动作,触发时机设为“收到通知时”

步骤2:在SwiftUI代码中发送通知触发动画

在你的SwiftUI视图中,通过NotificationCenter发送对应名称的通知,即可触发RCP中配置的时间线动作。示例代码如下:

import SwiftUI
import RealityKit
import Experience // 替换为你的RCP生成的模块名

extension Notification.Name {
    // 定义常量避免拼写错误
    static let spinAndMove = Notification.Name("SpinAndMove")
}

struct ContentView: View {
    @State private var loadedScene: Experience.Scene?

    var body: some View {
        VStack(spacing: 20) {
            Button("启动SpinAndMove动画") {
                // 发送通知触发时间线动作
                NotificationCenter.default.post(name: .spinAndMove, object: nil)
            }
            .buttonStyle(.borderedProminent)

            RealityView { content in
                // 加载RCP场景
                guard let scene = try? await Experience.loadScene() else { return }
                loadedScene = scene
                content.add(scene)
            }
        }
        .padding()
    }
}

最佳实践

  • 使用通知名称常量:将通知名定义为Notification.Name的扩展,避免手动输入字符串时出现拼写错误
  • 场景加载校验:在发送通知前可校验loadedScene是否不为空,确保场景已加载完成再触发动作
  • 区分多动作:如果有多个时间线动作,给每个动作分配唯一的通知名称,便于管理和维护
  • 自动生命周期管理:RCP的时间线动作会随场景生命周期自动处理通知监听,无需手动清理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 00:30:59