Join Jesse Freeman for an in-depth discussion in this video Introducing Texture2D, part of Unity 5 2D: Texture Rendering.
- [Instructor] Let's go ahead and create a new project in Unity. We're going to name this project Texture2DEditor. Next, we'll set this project to be 2D mode. We'll turn off Unity Analytics and you can save this file anywhere on your computer. When you're done, click the Create Project button. As you can see, Unity is now open in its default window layout.
Before we make any changes, let's go ahead and save the current scene we're in. Simply go to File, Save Scene, and we'll add this into the Assets folder. Let's call this Texture2DStaging. Next, let's open up the Resources folder in the Project files and drag our image in to use as a texture. As you can see in the Resources folder there's a pixel-vision-8-boot-screen.
We'll drag this over to Unity and it'll automatically be added to our project. Once it's been imported, select it, and we'll be able to see its settings in the Inspector. Any image that we add to our project is automatically converted into a Sprite texture type. While there are several different types of textures you can use in Unity, since we're in 2D mode, all textures will automatically be converted into Sprites. We're going to focus solely on this type for this course.
A texture represents any image that we can map onto a 3D surface. Since we're working in a 2D project, simply dragging this texture into the scene will automatically convert it into a Sprite. If we run the game you'll see our texture is now displayed on the screen. We can select a Camera and we can zoom in in order to get a better look at it. Now when it comes to pixel art textures, you'll notice that Unity's default settings actually compress the image a little bit too much.
Let's take a look at how we can clean this up so that the textures we run in this project will look pixel-perfect. We'll stop the player, select the texture, and we'll go over to the Filter Mode. Here we're going to change the Filter Mode to Point, which removes any filtering on the texture. And the next thing we need to do is remove the compression from the texture as well. Here we'll select None as the Compression type and click Apply.
If we rerun the project, select the Camera, and zoom in, you'll see that our image now looks pixel-perfect. But whenever we work with pixel art or any type of 2D texture inside of Unity, we're going to want to display it at its native size. Let's stop the player, select the texture again, and go over to the Pixels Per Unit. Here we're going to set this value to one and click Apply. As you can see, the image has now expanded and is much larger than the current display.
What we've done is told Unity to represent each single pixel as a single unit in the World Space. You can almost see how this works because each of the pixels are aligned to the grid inside of the scene view. If we rerun the game, you'll see the camera is very close to our texture. We can select the Camera and zoom out in order to get a better view. Now our texture is being displayed at its native resolution.
Before you move on, make sure to go ahead and save your project.
- Accessing Texture2D pixel data
- Using raw images
- Reading color data
- Detecting the position of a mouse
- Creating textures
- Drawing and erasing pixels