在 SpriteKit 中预加载纹理 [英] Preloading textures in SpriteKit

查看:32
本文介绍了在 SpriteKit 中预加载纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一些研究,但似乎找不到任何可以清楚地解释如何在动画中预加载单个纹理和纹理的内容.我目前正在使用 Assets.xcassets 中的 Atlas 对相关动画图像进行分组.将我的图像放在 Atlas 中是否意味着它们已预加载?至于单个图像,在 GameScene 之前声明纹理是否有意义: let laserImage = SKTexture(imageNamed: "Sprites/laser.jpg") 然后(例如)在我的 SKSpriteNode 子类之一中,我可以通过 laserImage 吗?

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 using Atlas's in Assets.xcassets to group related animation images. Does having my images in the Atlas mean that they are preloaded? As far as single images, does it make sense to declare the texture before GameScene like this: let laserImage = SKTexture(imageNamed: "Sprites/laser.jpg") and then (for example) within one of my SKSpriteNode subclass I can just pass laserImage through?

我最终想知道是否有明确定义的方法来解决这个问题,或者我是否应该在 GameScene 之前将每个纹理存储为常量.任何有关解决此问题的正确(和最有效)方法的建议都会很棒.

I ultimately wanted to know if there was a well defined way of going about this or if I should just store each texture as a constant before GameScene. Any advice on the proper (and most efficient) way of going about this would be great.

推荐答案

一个单一的纹理图集

将所有资源放入一个 Sprite Atlas.如果不合适,至少尝试将单个场景的所有资源放入单个 Sprite Atlas

One Single Texture Atlas

Put all your assets into a single Sprite Atlas. If they don't fit, try at least to put all the assets of a single scene into a single Sprite Atlas

如果您愿意,可以使用此代码将纹理图集预加载到内存中

If you want you can preload the texture atlas in memory with this code

SKTextureAtlas(named: "YourTextureAtlasName").preloadWithCompletionHandler { 
    // Now everything you put into the texture atlas has been loaded in memory
}

自动缓存

您不需要保存对纹理图集的引用,SpriteKit 有一个内部缓存系统.让它完成它的工作.

Automatic caching

You don't need to save a reference to the texture atlas, SpriteKit has an internal caching system. Let it do it's job.

忘记文件图像的名称,您在资产目录中分配给图像的名称是您唯一需要的名称.

Forget the name of the file image, the name you assign to the image into Asset Catalog is the only name you will need.

let texture = SKTextureAtlas(named:"croc").textureNamed("croc_walk01")
let croc = SKSpriteNode(texture: texture)

这篇关于在 SpriteKit 中预加载纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆