watchOS - SpriteKit 应用程序因 CPU 使用率过高而被终止

问题描述 投票:0回答:1

我的问题发生在 watchOS 上,但我想这是一个普遍的 SpriteKit 问题 所以...我有一个 watchOS 应用程序正在渲染 SpriteKit 场景(基本上是自定义 MapView)。当我的用户在手表上平移时,我更新 SpriteKit 的位置,当用户转动表冠时,我更新它的比例。

所以现在,当用户变得非常疯狂并疯狂地平移和缩放时,我的场景将不得不经常更新,这将导致“崩溃”,因为我超出了 CPU 限制。

e.g.:
CPU: 9 seconds cpu time over 20 seconds (46% cpu average), exceeding limit of 15% cpu over 60 seconds

当 sprit-nodes 离开屏幕时,我已经缓存了它们(并在我可能再次需要它们时重用它们,而不是仅仅删除它们),这使事情变得更好,但如果没有重复使用 sprite,CPU 使用率仍然会下降再次起来。 大部分时间都花在这里:

let texture = SKTexture(image: image)
let node = SKSpriteNode(texture: texture)

你会如何处理这个问题?一段时间后限制用户输入?或者是否有任何我尚未找到的 Info.plist 检查告诉 watchOS 如果我的应用程序使用更多一点就可以了?

ios swift swiftui sprite-kit watchos
1个回答
0
投票

本说明

let texture = SKTexture(image: image)

很贵,因为正如Apple 文档所述:

在控制权返回游戏之前复制图像数据。

纹理图集

相反,你应该这样利用

TextureAtlas

guard
    let enemyImage = UIImage(named: "panda.png"),
    let heroImage = UIImage(named: "hero.png")
else {
    fatalError()
}
let textureAtlas = SKTextureAtlas(dictionary: ["enemy": pandaImage,
                                               "hero": heroImage])

接下来当您需要使用您编写的特定纹理实例化 Sprite 时

guard let heroTexture = textureAtlas.textureNamed("hero") else { fatalError() }
let hero = SKSpriteNode(texture: heroTexture)

静态生成TextureAtlas

您还可以按照这些说明让 Xcode 为您填充TextureAtlas。

精灵图集

或者,您可以按照这些说明切换到更现代的 SpriteAtlas 技术。

注释

当然用更合适的错误管理替换

fatalError()

© www.soinside.com 2019 - 2024. All rights reserved.