Sprite Kit在OS X上以全屏模式出现严重的FPS问题 [英] Sprite Kit Serious FPS Issue In Full Screen Mode on OS X

查看:100
本文介绍了Sprite Kit在OS X上以全屏模式出现严重的FPS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个相当复杂的Sprite Kit游戏.我最近添加了对OS X的支持.无论窗口调整大小(即使将大小调整到最大屏幕空间)如何缩放,我始终都能获得60 fps.但是,当我让我的应用进入全屏"状态时,fps下降至30-40 fps,并保持这种状态吗?但是,如果在启用全屏的情况下拿起鼠标光标并显示菜单栏,则fps会回升至60 fps!

I'm making a fairly complex sprite kit game. I recently added support for OS X. I get 60 fps always, regardless of how my game is scaled when the window is resized (even when resized to max screen space). However, the moment I make my App enter "Full Screen," the fps drops to 30-40 fps and stays that way? But if I take my mouse cursor and reveal the menu bar while full screen is enabled, the fps goes back up to 60 fps!

您甚至可以通过使用默认模板在Xcode中为Mac制作Sprite Kit游戏来测试此错误.这是我为Mac默认游戏模板拍摄的屏幕截图.

You can even test this bug by making a sprite kit game for mac in Xcode using the default template. Here are the screen shots I took of the default game template for mac.

我建议您自己尝试一下,如果使用OS X的Apple默认Sprite Kit模板,您甚至不必编写任何代码.

I suggest trying it out for yourself, you don't even have to write any code if you use Apple's default sprite kit template for OS X.

最大窗口(无FPS问题:59-60 FPS)

全屏模式(FPS降至30-40 FPS)

全屏模式,鼠标位于顶部,显示菜单栏(令人惊讶的是,没有FPS问题:59-60 FPS)

任何人都不知道是什么原因导致了此问题.我不想以全屏模式发布我的应用程序,因为这意味着用户会失去性能.您可能会认为全屏模式可以更好地优化绘图,但是显然相反.我正在优胜美地上运行它.

Anyone have any idea what might be causing this problem. I don't want to release my App with full screen mode if it means users will lose performance. You would think full screen mode could better optimize drawing but apparently it's quite the opposite. I'm running this on Yosemite.

推荐答案

好吧,经过数周的研究,我已经找到了解决此问题的方法.在开始之前,让我开始解释我的设置.我在装有SKView的情节提要中使用NSViewController.我已经在MacBook Pro(15英寸,2013年初,视网膜)上测试了解决方法,但我不知道下面介绍的解决方法是否可以在其他Mac上使用.我相信应该在有机会的时候进行测试,看看下面的变通办法是否有效.

Ok, after weeks looking into this issue I have found some workarounds to this issue. Before I begin, let me start by explaining my setup. I'm using an NSViewController in a storyboard which holds an SKView. I've tested the workaround on MacBook Pro (Retina, 15-inch, Early 2013), I have no idea if the workarounds I present below will work on other Macs. I believe it should, when I get the chance I will test and see if the workarounds below work.

因此,在我开始之前,让我们回顾一下问题所在.问题在于,通过单击全屏按钮使您的应用进入全屏状态会导致FPS大幅下降.以下是启用全屏按钮的方式:

So before I begin, lets recap what the issue is. The issue is that making your App enter fullscreen by clicking the fullscreen button causes a massive drop in FPS. Below is how you enable the fullscreen button:

self.view.window!.collectionBehavior = .FullScreenPrimary

因此,我到处搜索,发现了使用此代码进入全屏显示的另一种方式:

So then I searched around and found a different way of entering fullscreen using this code:

 self.view.enterFullScreenMode(NSScreen.mainScreen()!, withOptions: nil)

但是我的FPS仍然大幅下降.请记住,在最大化窗口模式甚至菜单栏可见的全屏模式下,我都没有fps的问题! (请参阅有问题的图片).

But I still had a massive drop in FPS. Keep in mind, I had no fps issues when in maximized window mode or even full screen with the menu bar visible! (see pictures in question).

因此,我尝试了一种不太高级的方法来全屏显示.我在此处

So then I tried a less high-level approach to going full screen. I found a guide by Apple here

使用指南中的一些代码,我设法通过将窗口大小设置为显示器大小并将窗口定位在所有OS X UI上方来进入全屏模式.的代码如下:

Using some of the code from the guide, I managed to enter fullscreen by setting the window size to the size of the display, and positioning the window above all OS X UI. The code for this is as follows:

self.view.window!.styleMask = NSBorderlessWindowMask
self.view.window!.level = Int(CGWindowLevelForKey(Int32(kCGMainMenuWindowLevelKey))) + 1
self.view.window!.opaque = true
self.view.window!.hidesOnDeactivate = true
let size = NSScreen.mainScreen()!.frame.size
self.view.window!.setFrame(CGRect(x: 0, y: 0, width: size.width, height: size.height), display:true)

但是,可悲的是,同样的问题... FPS像以前一样下降了.

But, sadly, same problem... The FPS just dropped just like before.

所以我想如果我弄乱了窗户的大小/位置怎么办.因此,我尝试将窗口向下移动,以便仅显示菜单栏,如下所示. 且已完成.我的fps不再下降.但显然不是真正的全屏显示,因为菜单栏可见

So then I thought what if I mess with the size/position of the window. So I tried moving the window down so that just the menu bar was visible as shown below. AND THIS WORKED. I no longer had a drop in fps. But obviously it's not truly fullscreen because the menu bar is visible

self.view.window!.setFrame(CGRect(x: 0, y: 0, width: size.width, height: size.height-NSApplication.sharedApplication().mainMenu!.menuBarHeight), display:true)

实际上,事实证明,仅将窗口大小调整1点即可解决fps下降的问题. 因此,该错误必须与窗口大小与屏幕大小匹配时对苹果所做的优化(具有讽刺意味的苹果)有关.

In fact, as it turns out, just be adjusting the window size by 1 point fixes the drop in fps. Thus the bug must be related to an optimization (how ironic) apple does when your window size matches the screen size.

不相信我吗?这是链接的报价.

Don't believe me? Here is a quote from the link.

OS X v10.6及更高版本会自动优化性能 屏幕尺寸窗口

OS X v10.6 and later automatically optimize the performance of screen-sized windows

因此,要解决此问题,我们需要做的就是将窗口大小高度增加1个点,这将防止OS X尝试优化窗口.这将导致您的应用在顶部略微被割断,但1像素根本不会引起注意.而且在最坏的情况下,您可以将节点位置调整1点以解决此问题.

So to fix the issue all we need to do is make our window size height 1 point larger which will prevent OS X from trying to optimize our window. This will cause your App to get slightly cut off on the top but 1 pixel shouldn't be noticeable at all. And in the worst case you could adjust your nodes position by 1 point to account for this.

为方便起见,下面列出了两种解决方法.这两种变通办法都不会导致FPS下降.您的应用程序应该像在最大化窗口模式下一样运行.第一种解决方法是使您的应用全屏显示,并在顶部显示菜单栏.第二种解决方法是将您的应用置于没有菜单栏的完整全屏状态.

For your convenience, listed below are the 2 workarounds. Both of these workarounds do not cause any drop in FPS. Your App should function just like it did in maximized window mode. The first workaround puts your App in fullscreen and displays the menu bar at the top. The second workaround puts your App in complete full screen with no menu bar.

self.view.window!.styleMask = NSBorderlessWindowMask
self.view.window!.level = Int(CGWindowLevelForKey(Int32(kCGMainMenuWindowLevelKey))) + 1
self.view.window!.opaque = true
self.view.window!.hidesOnDeactivate = true

let size = NSScreen.mainScreen()!.frame.size
self.view.window!.setFrame(CGRect(x: 0, y: 0, width: size.width, height: size.height-NSApplication.sharedApplication().mainMenu!.menuBarHeight), display:true)

解决方法2:全屏显示,没有菜单栏

self.view.window!.styleMask = NSBorderlessWindowMask
self.view.window!.level = Int(CGWindowLevelForKey(Int32(kCGMainMenuWindowLevelKey))) + 1
self.view.window!.opaque = true
self.view.window!.hidesOnDeactivate = true
let size = NSScreen.mainScreen()!.frame.size
NSMenu.setMenuBarVisible(false)
self.view.window!.setFrame(CGRect(x: 0, y: 0, width: size.width, height: size.height+1), display:true)


如果由于某些原因这些解决方法不起作用,请尝试将窗口的大小/位置弄乱一些.另外,您可能需要更改窗口级别,具体取决于您是否有其他视图(例如,对话框),而您的应用程序不应重叠. 另外,请记住要向Apple提交错误报告.

这些解决方法使用NSBorderlessWindowMask.当键窗口更改时,这些类型的窗口不接受键盘输入.因此,如果您的游戏使用键盘输入,则应覆盖以下内容.请参见此处

These workarounds use an NSBorderlessWindowMask. These type of windows do not accept keyboard input when the key window changes. So if your game uses keyboard input, you should override the following. See here

 class CustomWindow: NSWindow {
    override var canBecomeKeyWindow: Bool {
        get {
            return true
        }
    }
    override var canBecomeMainWindow: Bool {
        get {
            return true
        }
    }
}


更新:一些坏消息

在Mac Book Air上测试了此变通办法,除非减去了大约100点(这显然非常引人注目),否则它不起作用.我不知道为什么. andyvn22的解决方案也是如此.我还注意到,很少,也许每60个版本启动一次,提供的解决方法根本就不适用于Mac Book Air.修复的唯一方法是重新启动该应用程序.也许Max Book Air是个特例.也许缺少显卡与该问题有关.希望苹果能解决这个问题.我现在在支持全屏和不支持全屏之间陷入了困境.我确实希望用户能够进入全屏模式,但与此同时,我也不想冒险让用户失去其FPS的一半.

Tested this workaround on Mac Book Air, and it did not work unless about 100 points were subtracted (which obviously is extremely noticeable). I have no idea why. Same goes for andyvn22's solution. I also have noticed that very rarely, perhaps once every 60 launches the workarounds provided simply don't work on the Mac Book Air at all. And the only way to fix is to relaunch the App. Maybe the Max Book Air is a special case. Maybe lack of a graphics card has to do with the issue. Hopefully Apple gets the issue sorted out. I'm now torn between supporting fullscreen and not supporting fullscreen. I really want users to be able to enter fullscreen mode, but at the same time I don't want to risk users loosing half their FPS.

这篇关于Sprite Kit在OS X上以全屏模式出现严重的FPS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆