SpriteKit紋理集
1烟很、SpriteKit紋理集的使用非常簡單谴垫,只需要將需要打包的圖片放到一個文件夾竖伯,以.atlas為后綴命名存哲,然后將文件夾拖入Xcode中即可:
項目中還可以設(shè)置紋理集的格式還有最大尺寸,這里最大尺寸有2048X2048和4096X4096兩個選項七婴,一般4096是移動設(shè)備上面OpenGL ES支持的最大尺寸祟偷。在代碼中使用紋理集也很簡單:
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"monster"];
SKTexture *f1 = [atlas textureNamed:@"monster-walk1.png"];
SKTexture *f2 = [atlas textureNamed:@"monster-walk2.png"];
SKTexture *f3 = [atlas textureNamed:@"monster-walk3.png"];
SKTexture *f4 = [atlas textureNamed:@"monster-walk4.png"];
NSArray *monsterWalkTextures = @[f1,f2,f3,f4];
2、紋理集的工作方式:實際上Xcode在打包的時候會幫你把.atlas文件夾中的所有圖片合圖打厘,圖片的大小會小于等于上圖中設(shè)置的最大尺寸修肠,如果圖片比較多,一個2048X2048(假設(shè)設(shè)置的最大尺寸是2048X2048)的大圖放不下户盯,那么會合成多張大圖嵌施。與此同時饲化,還會生成一個plist文件描述原來的圖片在合圖中的位置等信息。Xcode的這種工作方式在蘋果的OpenGLES_ProgrammingGuide中有下面的描述:
- Xcode can automatically build texture atlases for you from a collection of images. For details on creating a texture atlas, see Xcode Help. This feature is provided primarily for developers using the Sprite Kit framework, but any app can make use of the texture atlas files it produces. For each .atlas folder in your project, Xcode creates a .atlasc folder in your app bundle, containing one or more compiled atlas images and a property list (.plist) file. The property list file describes the individual images that make up the atlas and their locations within the atlas image—you can use this information to calculate appropriate texture coordinates for use in OpenGL ES drawing.
說的意思是Xcode會創(chuàng)建一個同名的.atlasc文件夾在包內(nèi)吗伤,這個文件夾里面包含了一個或多個合圖吃靠,外加一個.plist描述文件。蘋果直接說這個特性雖然是為SpriteKit開發(fā)的足淆,但是你不用SpriteKit也可以使用這個合圖巢块,比如說OpenGL ES。
3缸浦、直接編譯工程夕冲,打開bundle文件夾,確實有同名的.atlasc文件夾裂逐,下面有合圖的jpg文件歹鱼,還有.plist文件:
.plist文件里面包括各個圖片的位置信息,與TexturePacker輸出的格式文件非常相似卜高,我們大可以利用這一點在其他地方使用紋理集弥姻,不一定非要限制于SpriteKit,只是需要寫一些解析的方法就行了掺涛。至于說怎么解析庭敦,可以參考我之前的文章SpriteKit導(dǎo)入TexturePacker導(dǎo)出的紋理集類似的方式即可。
參考:
https://developer.apple.com/documentation/spritekit/sktextureatlas?language=objc
https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesForWorkingWithTextureData/TechniquesForWorkingWithTextureData.html#//apple_ref/doc/uid/TP40008793-CH104-SW1