Preloading textures in SpriteKit

后端 未结 2 1829
长情又很酷
长情又很酷 2021-02-02 03:19

I\'ve done some research and I can\'t seem to find anything that clearly explains how to go about preloading both single textures and textures within animations. I\'m currently

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 03:56

    I tried to implement appzYourLife answer but it increased the time to load every texture. So I ended up putting all the most used Sprites in one single atlas and puting it in a singleton.

    class Assets {
        static let sharedInstance = Assets()
        let sprites = SKTextureAtlas(named: "Sprites")
    
        func preloadAssets() {
            sprites.preloadWithCompletionHandler {
                print("Sprites preloaded")
            }
        }
    }
    

    I call Assets.sharedInstance.preloadAssets() in menuScene. And:

    let bg1Texture = Assets.sharedInstance.sprites.textureNamed("background1")
    bg1 = SKSpriteNode(texture: bg1Texture)
    

    to reference a texture already loaded in memory.

提交回复
热议问题