ios - 在 SpriteKit 项目中使用 Sprite 图集、纹理图集或 Assets 目录

标签 ios swift sprite-kit asset-catalog sktextureatlas

Apple 推荐的在 SpriteKit 项目中组织 Assets 的方式在引擎的历史进程中发生了很大的变化。旧方法中也存在错误,这些错误阻碍了这些实践和所需变通办法的实现。 SO 上的旧 Q&A 充满了过时的信息,或者被关于处理不再存在的错误的信息所混淆。需要有一篇关于 SO 的帖子,告诉您如何实现该问题的当前解决方案,而不会因讨论过时的工作流或已解决的错误而分心。所以问题是:

如何在 SpriteKit 项目中组织我的图像资源?一些 Apple 文档说到 use texture atlases to improve performance .但是 SKTextureAtlas class reference描述了创建纹理图集和 Sprite 图集的选项。我应该使用哪一个以及如何实现?

最佳答案

在 Assets 目录中创建 Sprite 图集。您可以获得拥有纹理图集的性能优势(旧方法是手动创建纹理图集),还可以轻松地在 Assets 目录中组织图像 Assets 。

In this WWDC video在时间戳 18:06,演讲者说:

Organize your image assets in the normal way in asset catalogs: group them, name them in a sprite atlas. And what this does is automatically create texture atlases at build time that you can retrieve through the SKTextureAtlas class.

这在 class reference doc 中得到了证实(虽然没有视频中那么清晰) ,它说:

The preferred mechanism to create a texture atlas is within an asset catalogue... A sprite atlas offers the advantages of a texture atlas with the management functionality of an asset catalog.

视频和类(class)引用文档还提到这种做法可以让您利用 app thinning (在视频中详细讨论)。

默认情况下,新的 Xcode 游戏项目是使用 Assets 目录创建的。或者,您可以通过选择 File > New > File... 然后从 Resource 中选择 Asset Catalog 向您的项目添加新的 Assets 目录> 出现的屏幕部分。

要添加 Sprite 图集,请首先在项目导航器中选择您的 Assets 目录。然后在编辑器区域的大纲 View 中右键单击或选择加号按钮 (+)。从出现的下拉列表中选择New Sprite Atlas

Create a new sprite atlas

您在 Sprite 图集中组织的图像资源将在构建时自动形成纹理图集。您可以通过代码从纹理图集中加载图像:

let atlas = SKTextureAtlas(named: "Monster")
let frame1 = atlas.textureNamed("monster-walk1.png")

关于ios - 在 SpriteKit 项目中使用 Sprite 图集、纹理图集或 Assets 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49866298/

相关文章:

懒人眼保健操之iOS SpriteKit分屏

ios - 在侧 View Controller 中创建幻灯片

ios - 如何通过任何iOS API查找当前季节?

swift - 画线动画

ios - 如何让用户复制 View

ios - 当应用程序使用 Facebook 登录时,SVProgressHud 未在 iOS 9 中显示

swift - 让物体从屏幕边缘反弹?

swift - 在 SpriteKit 中安排音频 - Swift

ios - 如何从json存储到sqlite数据库中

javascript - 文本选择事件结束?