我正在做一个SpriteKit游戏,想对它进行一下简介。SceneKit有一个非常有用的 showsStatistics
属性,展开后会显示出这样的图表。
我怎样才能获得像这样的SpriteKit游戏的每帧统计数据?我对Instruments不够了解,但就我所见,Time Profiler模板只显示累计函数时间,而Game Performance模板只显示Metal的渲染时间。
非常感谢。
我发现路标功能对此很有用。
在 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.comdocumentationspritekitsksceneresponding_to_frame-cycle_events。)
你也可以在适当的地方添加其他标志。 我把它们放在这里是为了标记各种游戏事件(例如,敌人产卵,玩家被击中等),并标记我怀疑对性能很重要的代码部分的启动。 在windowed模式下的仪器下运行,到了游戏有一些滞后问题的时候,然后停止追踪就可以了。 你可以看一下追踪的情况,看看是怎么回事。