Preload a Scene to prevent lag?

强颜欢笑 提交于 2019-12-25 01:19:28

问题


Hello guys i have made the first Level of my game, but always when i go from the main menu screen to the first level the screen freezes for like 2 Seconds and the transition from the Main screen to the game is very delayed and laggy and it sometimes doesn't even show up. Is there a way to preload the Scene in the background to prevent the lag?


回答1:


you can load the resources for the scene in a different thread. I do this in my game to get really snappy scene transitions despite the fact im loading tons of resources.

make a static function in your scene class to preload your scene

class func createResources(withCompletion: (scene: BaseScene) -> ()){

    // load resources on other thread
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {

        let scene = YourScene()

        // callback on main thread
        dispatch_async(dispatch_get_main_queue(), {
            // Call the completion handler back on the main queue.
            withCompletion(scene: scene)
        });
    })

}

call it like this

    YourScene.createResources(withCompletion: {
        [weak self]
        scene in

        self!.skView.presentScene(scene)
    })

So the way to use this is to build your scene in advance on the different thread. since its running on a different thread you shouldnt get that awkward pause.

for example. lets say the player reaches the goal of beating the level. before I was using this method the game would pause for a second before loading the next scene.

When the player beats the level now I still allow them to move around until the next scene has loaded and then the player will instantly shoot into the next level creating an instant transition.

you can see it here when the ship is hyperspacing between levels. there are a lot of resources loading but the transitions are seamless. https://www.youtube.com/watch?v=u_bXA3woOmo



来源:https://stackoverflow.com/questions/36990795/preload-a-scene-to-prevent-lag

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