Chapter 2. 2D Assets for Unity

In this chapter, we will cover the following recipes:

  • Importing textures and setting them to Inspector
  • Configuring transparency
  • Creating materials
  • Setting materials' names in Maya
  • Setting the ambient light in Unity
  • Texture atlases
  • Animated materials

Introduction

Whether your game is going to be 2D or 3D, you will deal with a number of 2D assets, mainly textures. Therefore, before building a game, I will cover this topic and explain how textures and other 2D assets are dealt within Unity.

Though we assume you are familiar with texturing-related matters and image file formats used for texturing games, we are going to describe the most relevant aspects of the operations throughout this chapter.

Some could say that textures should have been covered before importing 3D models into Unity. As a matter of fact, when you export a model from a 3D editor like Maya, the exporting process takes care of setting references between the exported model, its material, and the textures for those materials. Once the model is imported, the material instances are recreated in Unity; if the textures for those materials have already been imported into the project, the materials can actually be read by Unity and the model will (almost) look like it did in Maya.

If the textures have not been imported onto the other side, Unity will miss the references and the model will be displayed with a strong, unnatural, and pink diffused color.

If this is what you ended up with using the recipes described in Chapter 1, Importing 3D Models and Animations, then you just need to go through this chapter and reimport the models after the textures.

We decided to treat models before textures because it made sense to us to deal with models first and then deal with the textures for the materials to be applied to those models.

We hope it didn't cause too much trouble!

About textures and materials

Textures are 2D images that go into the materials that are applied to 3D models. They are responsible for the fine details featured by models: colors, bumps, reflections, and other features of the so-called skin of a 3D model.

Materials, on the other hand, can be considered as the actual skin of the 3D model; imagine it as a kind of very elastic and flexible coat that entirely covers the model geometry. Now assume that you can selectively paint this coat so it displays all the details of clothes, be it a suit, a tuxedo, fur, or whatever fits the game design.

This skin is versatile enough that artists can actually add many features to it to reproduce the effect of light bouncing on its surface, thus allowing some parts to look like leather and others like fur, metal, or a piece of a cartoon—anything you can think of.

This characteristic of materials is the result of a computation performed by a graphic program called a shader. A shader is a piece of code that defines how light interacts with the color and other characteristics of a surface in a game scene.

In case you need a summary of what we are saying here, we recommend you check out the interesting article at: http://www.informit.com/articles/article.aspx?p=2162089&seqNum=2.

From a quantitative point of view, a single 3D model usually requires more than one material (usually two) and a single material usually requires a couple textures; that's why the Assets/Textures folder in a Unity project is very likely to be the largest folder of the entire project!

File formats of textures

As a game requires a lot of textures, it is important to consider how to effectively manage them from a memory-management perspective. Indie and mobile game development (the reference business models we keep in mind as we write this book) especially require you to optimize memory management, as the overall weight of a game in bytes can become a decisive point for its popularity. The following section provides a few rules and hints for using textures in Unity.

Though images come in a different number of file formats in any range from very light to very heavy, when working on an indie project in Unity, it is very likely that you turn to just a couple of them. From Version 4.0, Unity supports PSD files, the native Photoshop file format. That means you can work on an image in Photoshop and then save it directly into a Unity scene. As we stated in Chapter 1, Importing 3D Models and Animations, when discussing Unity supporting native Maya files, this is a handy feature to speed up the process of exporting textures into Unity. Just remember that PSD files are flattened upon importing, so you only get the upper layer of the PSD file you save in Unity. On the other hand, the original PSD file in Photoshop won't be affected, so you can keep working on it and take advantage of layers.

The other very popular file type is PNG. PNG files are very light, so you can save memory and also store the information about image transparency. As such, they are used to create transparent materials. Another very good thing of PNG files is that it isn't a proprietary format and you can export and import PNG files with any 2D editor!

Unity supports many file formats, namely TIFF, Targa, and BMP. Discussing all of them goes beyond the scope of this chapter (and this entire fookbook, too!), so we won't go there. You can refer to this link to Wikipedia to start your own research in this field: http://en.wikipedia.org/wiki/Image_file_formats.

Finally, don't worry if you don't have an image library of your own; as usual, we provide the images required for the recipes of this chapter!

Optimizing textures

When preparing textures for a Unity project, there are a number of good practices that are recommended and a couple of rules that must be followed.

First of all, texture sizes should come in powers of two. A reasonable sequence of image sizes in pixels for games made today is 256, 512, 1024, 2048, 4096.

It is not mandatory that textures are square, too, though Unity kindly appreciates that! Whenever you can use square images, do it.

What Unity doesn't like at all, on the other hand, is textures larger than 4096 pixels; if you try to import an image that is larger than 4096 pixels on any of its dimensions, it will automatically reduce to a more comfortable size, most likely 1024 x 1024. Simply, the software doesn't allow you to set an image larger than 4096 pixels. On the other hand, you shouldn't worry about that; 4096 is a very high resolution image and it is more than enough for any (indie or mobile) game.

Many other techniques are available to game artists to optimize graphic performances while reducing the overall memory allocation. Mipmapping, for example, is a technique that improves performance. Using normal maps to improve the look of low-poly models is another. We suggest you research these topics if they are not familiar to you. This link from Wikipedia should provide you with everything you need to know about them: http://en.wikipedia.org/wiki/Texture_mapping.

Well then. Assuming you are clear with what we have discussed so far, let's jump into the core of this chapter and start importing textures into our Unity project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.137.117