How to cache or preload SKLabelNode font?

浪尽此生 提交于 2019-11-26 11:36:38

问题


I\'m making a Sprite Kit app and in my scene I added an SKLabelNode. I noticed a pretty large lag-spike when I load the SKScene. After profiling the app I found it came from creating an SKLabelNode with a papyrus font(though the font doesn\'t matter). When I remove the label the scene starts up almost instantaneously, but with the label it takes an extra 1-3 seconds.

I am pretty sure it\'s from loading the font as when I go back to the main menu and play the game again it starts up instantly again.

Now is there a way to preload the font earlier so when the player selects the level there isn\'t a large pause?


回答1:


We had this issue, and it turned out that we were simply not using the "correct" font name. In our case, we were using "Menlo" instead of "Menlo-Regular" when instantiating our SKLabelNode, and it was incurring the several-second penalty. Once we used the correct font name, the delay no longer happened.

(Curiously, the SKLabelNode still found Menlo and used it, so it was not immediately obvious that we had the wrong font name. Presumably, the delay is caused by the system having to figure out the appropriate substitute to use. It does a good job, finding the font we intended to use, but it takes a while to do it, hence the delay.)




回答2:


I had the same issue. Add following code to your very first scene, with your font name:

SKLabelNode *preload = [SKLabelNode labelNodeWithFontNamed:@"Avenir"];
preload.text = @"text";

If you don't provide text it won't load font. Note that you don't need to add label as child to your scene.



来源:https://stackoverflow.com/questions/20446505/how-to-cache-or-preload-sklabelnode-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!