我发现路标功能对此很有用。
在logging.swift:
import Foundation
import os.log
extension OSLog {
static var subsystem = Bundle.main.bundleIdentifier!
static let poi = OSLog(subsystem: subsystem, category: .pointsOfInterest)
}
那么对于场景:
class MyScene: SKScene {
let signpostID = OSSignpostID(log: .poi)
override func update(_ currentTime: TimeInterval) {
os_signpost(.begin, log: .poi, name: "1_update", signpostID: signpostID)
// update code here
endOfUpdate()
}
/// Mark the end of the update phase and the start of actions
func endOfUpdate() {
os_signpost(.end, log: .poi, name: "1_update", signpostID: signpostID)
os_signpost(.begin, log: .poi, name: "2_actions", signpostID: signpostID)
}
/// Mark the end of actions and the start of physics computations
override func didEvaluateActions() {
os_signpost(.end, log: .poi, name: "2_actions", signpostID: signpostID)
os_signpost(.begin, log: .poi, name: "3_physics", signpostID: signpostID)
}
/// Mark the end of the render loop
override func didFinishUpdate() {
os_signpost(.end, log: .poi, name: "3_physics", signpostID: signpostID)
}
}
然后使用兴趣点工具;我通常会将其添加到游戏性能配置文件中。您将为渲染循环的每个阶段标记出区域。 (我从未使用过约束,因此更新的结束和物理模拟的结束在我的情况下是重合的,上面没有标记,但如果你需要捕获更多,你可以覆盖 didSimulatePhysics 和 didApplyConstraints那里有详细信息。请参阅https://developer.apple.com/documentation/spritekit/skscene/responding_to_frame-cycle_events 处的渲染循环描述)
您还可以在适当的位置添加其他路标。我将它们用于标记各种游戏事件(例如,敌人生成、玩家被击中等)并标记我怀疑对性能很重要的代码部分的开始/结束。以窗口模式在仪器下运行,到达游戏有一些滞后问题的地步,然后停止跟踪。您可以查看跟踪以了解发生了什么。