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

查看:280
本文介绍了在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天全站免登陆