2D游戏中对spritesheet的处理_unity

2d sprite 层级

2D Toolkit is an efficient and fast 2D sprite and text system developed by Unikron Software. The product provides heaps of features for mobile performance and workflow efficiency. In this tutorial we’ll introduce the new Sprite Collection Editor interface, and go through some of the optimization features provided by 2D Toolkit.

2D Toolkit是 Unikron Software 开发的高效,快速的2D Sprite和文本系统 。 该产品提供了大量功能,以提高移动性能和工作流程效率。 在本教程中,我们将介绍新的Sprite Collection Editor界面,并介绍2D Toolkit提供的一些优化功能。

(Installation)

Once you download 2D Toolkit from the Asset Store and import it into your project, you should be ready to go. You can upgrade directly from the Asset Store – if you’re using JavaScript, follow the steps below to get everything going again!

从Asset Store下载2D Toolkit并将其导入到项目后,就可以开始使用了。 您可以直接从Asset Store进行升级–如果您使用的是JavaScript,请按照以下步骤操作,以恢复一切!

(What if I need to use JavaScript?)

2D Toolkit is distributed as a full source package, and we’d like to keep it that way – it’s great being able to dissect the source code, and customize it to your needs. There is a minor inconvenience if you’re using JavaScript – you will need to run to run the Setup for JavaScript menu command under the 2D Toolkit menu. This will move the script files to the correct directory structure to be accessible from JavaScript.

2D Toolkit是作为完整的源代码包分发的,我们希望保持这种状态-能够剖析源代码并根据需要进行自定义非常好。 如果使用JavaScript,会带来一些不便-您需要运行以运行2D Toolkit菜单下的Setup for JavaScript菜单命令。 这会将脚本文件移动到正确的目录结构,以便可以从JavaScript进行访问。

If you’re upgrading from a previous version, make sure you restart Unity after importing the latest package, and run the Setup for JavaScript command once Unity has restarted.

如果要从以前的版本升级,请确保在导入最新软件包后重新启动Unity,并在Unity重新启动后运行“ JavaScript安装程序”命令。

(What is a sprite collection?)

A sprite collection is simply what the name says on the can – a collection of sprites. These sprites are run through a sequence of processes by 2D Toolkit and finally packed into one or more atlases and geometry cached for them. Sprite geometry doesn’t necessarily have to be square, which lets us do very interesting optimizations, some of which we will cover below.

精灵集合就是罐子上的名字,即精灵的集合。 这些精灵由2D Toolkit进行一系列处理,最后打包成一个或多个为其存储的地图集和几何。 Sprite的几何形状不一定必须是正方形的,这使我们可以进行非常有趣的优化,下面将介绍其中的一些。

(Creating a new sprite collection)

First, we’ll start by creating a directory to host the sprite collection. This isn’t necessary, but it’s good practice. Select the directory, and click on Project Window > Create > tk2d > Sprite Collection.

首先,我们将创建一个目录来托管sprite集合。 这不是必需的,但这是一个好习惯。 选择目录,然后单击项目窗口>创建> tk2d> Sprite Collection。

Once you have created the Sprite Collection, it is important to name it so you’ll be able to quickly select it later. We called ours “Tutorial Sprites”. Click on Open Editor in the inspector to bring up the Sprite Collection Editor window.

创建Sprite Collection之后,对其进行命名很重要,这样以后您就可以快速选择它。 我们称其为“ Tutorial Sprites”。 在检查器中单击“打开编辑器”以打开“精灵集合编辑器”窗口。

It’s a good idea to dock this window so you will be able to drag textures into it. What we will do now is drag a few textures as shown below. The sprites we are using are from /Assets/TK2DROOT/tk2d_demo/demospritecollection/sprites, which you should have if you imported the full package. Simply shift select all the textures you’d like to import and drag them to the Drop sprites here box as shown below.

停靠此窗口是个好主意,这样您就可以将纹理拖动到其中。 我们现在要做的是拖动一些纹理,如下所示。 我们正在使用的精灵来自/ Assets / TK2DROOT / tk2d_demo / demospritecollection / sprites,如果您导入了完整的软件包,则应该具有它们。 只需移位选择所有要导入的纹理,然后将它们拖到“将子像素放置在此处”框中,如下所示。

The newly dropped textures will automatically be selected in the Sprite Collection editor. You can now perform batch changes to any of the parameters you see in the inspector panel to the right.

新放置的纹理将在Sprite Collection编辑器中自动选择。 现在,您可以对在右侧检查器面板中看到的任何参数进行批量更改。

Once you are happy with your sprite collection, click on Commit to save and create the atlases. If you used the same textures we did, you’ll be presented with the following message – “Unable to fit textures in requested atlas area”.

对Sprite集合满意后,单击Commit保存并创建地图集。 如果使用与我们相同的纹理,将显示以下消息:“无法在请求的图集区域容纳纹理”。

The default atlas size is 1024, so we’ll increase this to 2048 temporarily to allow the textures to fit. Click on Settings and change Max Atlas Size to 2048, and click Commit again. You should see that the created atlas is 1024×2048 (10.7 MB), and there is 76.94% wasted space in it! This clearly isn’t a good use of texture space so we’ll try to fix this.

默认的地图集大小为1024,因此我们将暂时将其增加到2048,以使纹理适合。 单击设置,并将最大地图集大小更改为2048,然后再次单击提交。 您应该看到创建的地图集为1024×2048(10.7 MB),其中浪费了76.94%的空间! 显然,这不是纹理空间的好用法,因此我们将尝试解决此问题。

Click on atlas0_material in the materials foldout to locate it in the Project window, and then click on atlas0 in the same folder to view the texture. We’ve spotted the problem. There is one rather huge texture in the atlas.

在材料折页中单击atlas0_material,以在“项目”窗口中找到它,然后在同一文件夹中单击atlas0以查看纹理。 我们发现了问题。 地图集中有一个相当大的纹理。

Turns out this texture is used as a background texture, and has quite a bit of empty space (0 alpha transparent regions) in it and as such is a perfect candidate for Sprite Dicing.

原来,此纹理被用作背景纹理,并且其中有很多空白空间(0个alpha透明区域),因此是Sprite Dicing的理想选择。

(Sprite dicing)

Sprite dicing is a feature unique to 2D Toolkit which allows the system to split up a large texture into much smaller chunks, individually trimming these chunks, and seamlessly reconstructing the sprite in the viewport.

子画面切割是2D Toolkit独有的功能,它允许系统将大纹理分割成更小的块,分别修剪这些块,并在视口中无缝地重建子画面。

Select bg1 in the Sprite Collection Editor. Tick Dice in the inspector for this texture. The 2 parameters below are the chunk sizes to split the original texture into. Click on Commit with these default values, and look at the atlas texture again. It has now been reduced to 512×1024 (2.7MB), which is significantly smaller than the original texture. You can experiment with the 2 dice parameters to see what produces the best results.

在Sprite Collection Editor中选择bg1。 在检查器中勾选“ Dice”以获取此纹理。 以下2个参数是将原始纹理分割成的块大小。 单击使用这些默认值提交,然后再次查看地图集纹理。 现在,它已减小到512×1024(2.7MB),大大小于原始纹理。 您可以尝试使用2个骰子参数来查看产生最佳结果的参数。

Sprite dicing not only saves atlas space, but also fillrate and is a useful optimization on mobile platforms.

Sprite划片不仅可以节省地图集空间,而且可以节省填充率,并且在移动平台上非常有用。

(Creating sprites in the viewport)

The sprite collection has been created by default to display pixel perfect on a 960×640 display, with an orthographic camera with a size of 1. This is set up in the Settings overlay in the Sprite Collection editor, and you may change them if you’re targeting a different resolution.

默认情况下,创建了精灵集合,以在大小为1的正交摄影机上在960×640显示器上完美显示像素。这是在“精灵集合”编辑器的“设置”覆盖图中设置的,如果您针对不同的分辨率。

In this tutorial, however, we’ll simply use the defaults and set up a camera to match this.

但是,在本教程中,我们将仅使用默认值并设置一个与之匹配的摄像机。

The background sprite we diced in the previous step should appear in the viewport. Zooming in closer shows what 2D Toolkit has done to optimize this sprite. (Image contrast increased to highlight polygons). The sprite has been broken down into smaller chunks, and the empty (0 alpha) space removed.

我们在上一步中切块的背景精灵应该出现在视口中。 放大可以显示2D Toolkit为优化此Sprite所做的工作。 (图像对比度增加以突出显示多边形)。 该子画面已细分为较小的块,并删除了空白(0 alpha)。

This is an incredibly powerful optimization – but 2D Toolkit 1.70 introduces another feature which lets you take this further.

这是一项令人难以置信的强大优化-但是2D Toolkit 1.70引入了另一个功能,使您可以进一步进行这项工作。

(Custom Sprite Shapes)

Sprite dicing solves one set of problems, and is limited to shapes where the sprites can be reliably diced along the x and y axes. In some cases (hex and isometric tilesets), it may not work as well. Lets see what we can do to optimize the following sprite – it’s called “starspike” and should be in your sprite collection if you’ve followed the steps so far.

子画面切割可解决一组问题,并且仅限于可以沿x和y轴可靠地进行子画面切割的形状。 在某些情况下(十六进制和等距图块集),它可能无法正常工作。 让我们来看看如何优化下面的Sprite,它被称为“ starspike”,如果您已经按照目前的步骤进行操作,它应该位于您的Sprite集合中。

Select it in the Sprite Collection Editor, and now, tick “Custom Shape”. You should see a green outline around the sprite. Hint – you can use the mousewheel / two finger swipe gesture to zoom in to the sprite preview area.

在Sprite Collection Editor中将其选中,然后在“ Custom Shape”上打勾。 您应该在精灵周围看到一个绿色的轮廓。 提示–您可以使用鼠标滚轮/两指滑动手势放大到图片预览区域。

What we can do now is modify this geometry to tightly fit the sprite.

现在,我们可以做的就是修改此几何图形以使其紧贴精灵。

Click Commit, create the sprite in the scene view. You’ll find that it looks like this (triangle edges enhanced) – While this approach hasn’t saved any texture space, it has saved tremendous amounts of fillrate – always a good thing on mobile platforms.

单击“提交”,在场景视图中创建精灵。 您会发现它看起来像这样(增强了三角形边缘)–尽管这种方法没有节省任何纹理空间,但却节省了大量填充率–在移动平台上总是一件好事。

Using custom sprite shapes also allows you to make sprites “solid” instead of semitransparent. This can be an important optimization for tilemaps. Consider the following annotated screenshot. The tilemap editor seamlessly works with these sprite collections and creates splits in meshes where necessary for transparent materials and opaque materials. That brings us to the last topic in this tutorial.

使用自定义的精灵形状还可以使精灵成为“实体”,而不是半透明的。 这对于平铺图可能是重要的优化。 考虑以下带注释的屏幕截图。 tilemap编辑器可无缝处理这些sprite集合,并在需要透明材料和不透明材料的地方在网格中创建拆分。 这使我们进入了本教程的最后一个主题。

(Material Overrides)

When working mainly with tilemaps, and in other scenarios too, it may be beneficial to make some sprites solid where possible. Material overrides lets you do just that – it lets you share the same atlas, but use different materials for some of the sprites.

在主要使用图块贴图时,以及在其他情况下,尽可能使某些子图更坚固也可能是有益的。 材质替代可以让您做到这一点–它可以共享相同的图集,但对某些子画面使用不同的材质。

You can create a material override by clicking on the “+” button next to the Materials dropdown. This creates a duplicate of the current material, which you can then edit and reconfigure as you see fit. You will then be able to override individual or multi-selected sprites to use this material.

您可以通过单击“材料”下拉菜单旁边的“ +”按钮来创建材料替代。 这将创建当前材料的副本,然后您可以根据需要对其进行编辑和重新配置。 然后,您将可以覆盖单个或多个选定的精灵,以使用此材质。

(Conclusion)

We’ve covered only some of the powerful optimization opportunities in 2D Toolkit. This great product provides many more features which make it easy and efficient to make your game.

我们仅介绍了2D Toolkit中一些强大的优化机会。 这款出色的产品提供了更多功能,可让您轻松高效地制作游戏。

2D Toolkit: http://www.unikronsoftware.com/2dtoolkit

2D工具包: http : //www.unikronsoftware.com/2dtoolkit

Documentation and more tutorials: http://www.unikronsoftware.com/2dtoolkit/doc/

文档和更多教程: http : //www.unikronsoftware.com/2dtoolkit/doc/

Support forum: http://www.unikronsoftware.com/2dtoolkit/forum/

支持论坛: http : //www.unikronsoftware.com/2dtoolkit/forum/

翻译自: https://blogs.unity3d.com/2012/05/22/tutorial-sprite-optimizations-using-2d-toolkit/

2d sprite 层级