有没有一种方法可以在SpriteKit Swift中读取/获取fps?

5

有没有一种方法在Swift中获取FPS值?

不,我不是指改变frameInterval,也不是指...

skView.showsFPS = true

我希望能获取fps,以便在游戏开始之前,我的应用程序等待fps达到60。由于某种原因,我的应用程序偶尔会在40fps处“卡住”,停留5秒钟,然后慢慢攀升回到60。这通常发生在我召唤ControlCenter时。


1
这没有意义。你需要找出为什么你的应用程序需要一段时间才能启动,而不是通过检查FPS值来绕过问题。 - Bryan Chen
1
我正在尝试自己解决。我已经浏览了多个类似的stackoverflow问题,但是没有真正明确的答案解释它为什么会这样。适用于其他用户的解决方案在我的情况下并不适用。我也更愿意知道它为什么这样做,而不是寻找解决方法,但是在调试了许多天后,我仍然一无所知。 - C. Wagner
2
好问题。在游戏开发中,随时需要知道你的FPS是非常正常的。 - Fattie
3个回答

8
你可以自己计算。
在你的场景中执行以下操作:
var lastUpdateTime: TimeInterval = 0

func update(currentTime: TimeInterval) {
    let deltaTime = currentTime - lastUpdateTime
    let currentFPS = 1 / deltaTime
    print(currentFPS)

    lastUpdateTime = currentTime
}

0

有另一种方法可以获取视图本身的FPS,而不受呈现哪个场景的影响,使用SKViewDelegate(从开发者文档中获取,只需添加FPS属性):


class ViewController: NSViewController, SKViewDelegate {
    
    let spriteView: SKView = SKView()
    
    override func viewDidLoad() {
        self.spriteView.delegate = self // Set the view's delegate to the ViewController.
    }

    let targetFps: TimeInterval = 60 // Set to your preferred fps
    var lastRenderTime: TimeInterval = 0
    var fps: TimeInterval = 0.0 // Access this property to get the view's fps
    
    // Delegate method:
    public func view(_ view: SKView, shouldRenderAtTime time: TimeInterval) -> Bool {
        
        if time - lastRenderTime >= 1 / targetFps { // Check if the scene should render.
            
            // Determine the fps with the time since the last render:
            self.fps = 1 / (time - lastRenderTime)
            // Set the last render time to the current time:
            self.lastRenderTime = time
            // Let scene know that it should render the next frame.
            return true

        }
        else {
            return false
        }
        
    }
}


-3

gameView.showsStatistics = true

游戏视图.显示统计信息 = 真


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接