- Unity 2018 Shaders and Effects Cookbook
- John P. Doran Alan Zucconi
- 511字
- 2025-04-04 16:34:20
How to do it...
Adding a texture to your model using the Standard Shader is incredibly simple, as follows:
- In the Sample Code for this chapter that's provided with this book, you can find the basicCharacter model which, by default, has UV information embedded into it, making it so that when we attach a material, it will draw the texture using that information.
- Create a new Standard Surface Shader called TexturedShader by going to the Project tab and then selecting Create | Shaders | Standard Surface Shader. Once created, you can type in a new name for the shader and then press Enter.
- For organization's sake, open the shader and change the first line to the following:
Shader "CookbookShaders/Chapter03/TexturedShader" {
- This will allow us to find the shader inside of the organization we have been using so far for this book.
- Create a new material called TexturedMaterial by going to the Project tab and then selecting Create | Material. Once created, you can type in a new name for the material and then press Enter to confirm the change.
- Assign the shader to the material by going to the Inspector tab and then clicking on the Shader dropdown before selecting CookbookShaders/Chapter 03/TexturedShader:

You may also do this by first selecting the material and then dragging the shader file over it in the Project tab.
- After selecting the material, drag your texture to the empty rectangle called Albedo (RGB). If you are missing some, there are textures located in this chapter's example code that can be used. If you have followed all of these steps correctly, your material Inspector tab should look like this:
The Standard Shader knows how to map a 2D image to a 3D model using its UV models and textures used in this example were created by Kenney Vleugels and Casper Jorissen. You can find these and many other public domain game assets at Kenney.nl.
- To see the UV data in action, in the example code's Models folder, drag and drop the model into the Hierarchy tab. Once there, double-click on the newly created object to zoom in so that you can see the object:

- Once there, you may go to the Project tab and open up the Chapter 3 | Materials folder and drag and drop our Textured Material onto the character. Note that the model consists of different objects, each of which provides direction to draw in a particular place. This means that you will need to drop the material on each part of the model (ArmLeft1, ArmRight1, Body1, and so on); trying to apply this to the top level of the hierarchy only (basicCharacter) will not work:

- It is also possible to change what the object looks like by changing the texture that's being used. For instance, if we use the other texture provided (skin_womanAlternative), we have a very different looking character:

This is often used in games to provide different kinds of characters with minimal cost.