The customization of avatars usually includes the selection of one or more textures that define its looks. In this recipe, we will implement a GUI that allows the player to create their avatar by combining two texture channels.
The 3D object and image files needed for this recipe are included in the selectTexture
package, available inside the 0423_03_07
folder.
To overlay textures, follow these steps:
selectTexture
into your project.selectableMaterial
.face1
as the Base map and prop1
as the Decal map, as shown in the following screenshot:Avatar
game object. You can do this by dragging the material from the Project view into the game object's name in the Hierarchy view.SelectTexture
.SelectTexture
in your script editor and replace everything with the following code:using UnityEngine; using System.Collections; public class SelectTexture : MonoBehaviour { public Texture2D[] faces; public Texture2D[] props; void OnGUI() { for (int i = 0; i < faces.Length; i++) if (GUI.Button(new Rect(0, i * 64, 128, 64), faces[i])) ChangeMaterial("faces", i); for (int j = 0; j < props.Length; j++) if (GUI.Button(new Rect(128, j * 64, 128, 64), props[j])) ChangeMaterial("props", j); } void ChangeMaterial(string category, int index) { if (category == "faces") renderer.material.mainTexture = faces[index]; if (category == "props") renderer.material.SetTexture("_DecalTex", props[index]); } }
Avatar
game object.2
.The script allows the user to create two collections of textures: one for the Base map (named Faces) and another one for the Decal map (named Props). When the scene is played, the textures are displayed inside GUI buttons which can be used to change the texture in the Avatar
game object's material by calling the ChangeMaterial
function. This function will receive, as parameters, both the category (either Face or Prop) and index of the image, assigning the correspondent texture map to the appropriate texture channel.
This recipe could be easily adapted to change other parameters of different material shaders. Check Unity's online documentation to learn about other texture names at http://unity3d.com/support/documentation/ScriptReference/Material.SetTexture.html. Also, you might want to learn more about shaders by exploring the built-in shaders' source available at http://unity3d.com/support/resources/assets/built-in-shaders.
18.226.251.206