简体   繁体   English

在SpriteKit项目中使用精灵图集,纹理图集或资产目录

[英]Using a sprite atlas, texture atlas, or asset catalog in SpriteKit projects

Apple's recommended way for organizing assets in a SpriteKit project has changed greatly over the course of the engine's history. 苹果公司在SpriteKit项目中组织资产的推荐方式在引擎的历史过程中发生了很大变化。 There have also been bugs in the old methodology which stymied the implementation of these practices and required workarounds. 旧方法中还存在一些错误,这些错误阻碍了这些做法的实施和所需的解决方法。 The old Q&A on SO is full of information that is outdated or muddled by information about dealing with bugs that no longer exist. 关于SO的旧Q&A充满了有关处理不再存在的错误的信息的过时或混乱的信息。 There needs to be a post on SO that tells how to implement the current solution to this question without being distracted by discussion about outdated workflows or resolved bugs. SO上需要有一篇帖子,告诉您如何实施该问题的当前解决方案,而不会被过时的工作流或已解决的错误的讨论所干扰。 So the question is this: 所以问题是这样的:

How do I organize my image assets in a SpriteKit project? 如何在SpriteKit项目中组织图像资产? Some Apple docs say to use texture atlases to improve performance . 一些苹果公司的文档说使用纹理图集来提高性能 But the SKTextureAtlas class reference describes options for creating both texture atlases and sprite atlases. 但是SKTextureAtlas类参考描述了用于创建纹理地图集和子画面地图集的选项。 Which one should I use and how do I implement it? 我应该使用哪一个以及如何实现它?

Create a sprite atlas inside of an asset catalog. 在资产目录内创建一个精灵地图集。 You get the performance benefits of having a texture atlas (the old way was to manually create a texture atlas) and also the ease of organizing your image assets in an asset catalog. 您可以获得拥有纹理图集的性能优势(旧方法是手动创建纹理图集),并且还可以轻松地在资产目录中组织图像资产。

In this WWDC video at timestamp 18:06, the speaker says: 时间戳为18:06的WWDC视频中,发言人说:

Organize your image assets in the normal way in asset catalogs: group them, name them in a sprite atlas. 以常规方式在资产目录中组织图像资产:将它们分组,并在Sprite Atlas中命名它们。 And what this does is automatically create texture atlases at build time that you can retrieve through the SKTextureAtlas class. 这样做是在构建时自动创建纹理图集,您可以通过SKTextureAtlas类检索该纹理图集。

This is confirmed (although less clearly than in the video) in the class reference doc , where it says: 这在类参考文档中得到了确认(尽管与视频中的描述不一样):

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. 创建纹理图集的首选机制是在资产目录中。精灵图集提供了具有资产目录管理功能的纹理图集的优点。

Both the video and the class reference doc also mention that this practice lets you take advantage of app thinning (discussed at length in the video). 视频和课程参考文档都提到了这种做法,您可以利用应用程序精简 (在视频中进行了详细讨论)。

By default, new Xcode game projects are created with an asset catalog. 默认情况下,将使用资产目录创建新的Xcode游戏项目。 Or, you can add a new asset catalog to your project by selecting File > New > File... and then selecting Asset Catalog from the Resource section in the screen that appears. 或者,可以通过选择“ 文件”>“新建”>“文件...” ,然后从出现的屏幕的“ 资源”部分中选择“ 资产目录” ,将新的资产目录添加到项目中。

To add a sprite atlas, first select your asset catalog in the project navigator. 要添加精灵图集,请首先在项目导航器中选择资产目录。 Then either right-click or select the plus button (+) in the outline view of the editor area. 然后右键单击或在编辑器区域的大纲视图中选择加号按钮(+)。 Select New Sprite Atlas from the drop-down list that appears. 从出现的下拉列表中选择“ 新建Sprite地图集 ”。

创建一个新的精灵图集

Image assets that you organize in sprite atlases will be automatically formed into a texture atlas at build time. 您在精灵图集中组织的图像资产将在构建时自动形成纹理图集。 You can load images from a texture atlas via code: 您可以通过以下代码从纹理图集加载图像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM