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

如何阻止SceneKit加载USDZ模型时自动立即播放自带动画

解决方案

你可以通过两种常用方式实现USDZ模型加载后不自动播放动画,优先推荐第一种加载阶段配置的方案,性能开销更低:

方式1:加载场景时通过配置参数直接禁止自动播放

在初始化SCNScene时传入加载配置,指定动画导入策略为不自动播放即可,修改后的代码如下:

import PlaygroundSupport
import SceneKit

let modelURL = Bundle.main.url(forResource: "model", withExtension: "usdz")!
// 配置加载选项,指定动画导入后不自动运行
let loadOptions: [SCNSceneSource.LoadingOption : Any] = [
    .animationImportPolicy: SCNSceneSource.AnimationImportPolicy.doNotPlay
]
let scene = try! SCNScene(url: modelURL, options: loadOptions)

let view = SCNView(frame: CGRect(x: 0,
                                 y: 0,
                                 width: 500,
                                 height: 500))

view.scene = scene

PlaygroundPage.current.liveView = view

方式2:加载完成后手动暂停/移除所有动画

如果你需要保留动画后续手动触发,可以在场景加载完成后遍历所有节点,暂停或移除已绑定的动画:

// 将scene赋值给SCNView前执行以下逻辑
scene.rootNode.enumerateChildNodes { node, stop in
    // 方案A:直接移除所有动画,后续需要播放时要重新绑定
    node.removeAllAnimations()
    
    // 方案B:仅暂停动画,后续可随时调用play()恢复播放
    // node.animationKeys.forEach { key in
    //     node.animationPlayer(forKey: key)?.pause()
    // }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 11:09:02