It seems that SpriteKit doesn't batch the draw calls for textures in the same texture atlas. There are two different behaviors, based on how the SKTextureAtlas
gets initialized:
-
The draw calls are not batched when the texture atlas is initialized using
SKTextureAtlas(named:)
(loading the texture atlas from data stored in the app bundle). -
But the draw calls seem to be batched when the texture atlas is created dynamically using
SKTextureAtlas(dictionary:)
.
The following images show the two different behaviors and the SpriteAtlas inside the Assets Catalog.
1. Draw calls not batched:
2. Draw calls batched:
The SpriteAtlas:
I created a sample Xcode 13 project to show the different behavior: https://github.com/clns/spritekit-atlas-batching.
I have tried this on the iOS 15 simulator on macOS Big Sur 11.6.1.
The code to reproduce is very simple:
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
let atlas = SKTextureAtlas(named: "Sprites")
// let atlas = SKTextureAtlas(dictionary: ["costume": UIImage(named: "costume")!, "tank": UIImage(named: "tank")!])
let costume = SKSpriteNode(texture: atlas.textureNamed("costume"))
costume.setScale(0.3)
costume.position = CGPoint(x: 200, y: 650)
let tank = SKSpriteNode(texture: atlas.textureNamed("tank"))
tank.setScale(0.3)
tank.position = CGPoint(x: 500, y: 650)
addChild(costume)
addChild(tank)
}
}
Am I missing something?