Creating and Changing Materials and Textures in Unity
Unity Material Customization Cube Plane Sphere 2025 – In this practical, you will create a simple scene in Unity that contains three basic 3D objects: a Cube, a Plane, and a Sphere. You will then apply separate materials and textures to each object, and finally, write a C# script to dynamically change the color and texture of each GameObject when a button is clicked.
Theory
In Unity, a material is what determines the appearance of a 3D object in terms of color, texture, and how light interacts with it (such as whether it’s shiny, matte, transparent, etc.). A texture is an image applied to the surface of a 3D model to give it more detail, such as wood, metal, or any other surface pattern.
Here’s the breakdown:
- Cube: A 3D shape with six square faces.
- Plane: A flat surface that is commonly used as a ground in 3D scenes.
- Sphere: A round object.
In Unity, materials and textures can be applied to objects via the Renderer component, which controls how the object is displayed.
Unity Material Customization Cube Plane Sphere 2025
Requirements
- Unity 3D: You need Unity installed on your computer. You can download it from Unity’s official site.
- Visual Studio: You’ll need Visual Studio for writing and managing C# scripts. It integrates with Unity for writing scripts and debugging.
- Basic Unity Knowledge: You should be familiar with the Unity interface and creating objects in the scene.
Steps to Create the Scene
- Open Unity and Create a New Project
- Launch Unity and create a new 3D project.
- Add the 3D Objects to the Scene
- Go to
GameObject
in the top menu, then select3D Object
. Add a Cube, Plane, and Sphere to your scene. - You should now have these three objects visible in the Scene view.
- Go to
- Create Materials
- In Unity’s Assets folder, right-click and select Create > Material.
- Name the material for each object (e.g., CubeMaterial, PlaneMaterial, SphereMaterial).
- You can change the Color by selecting the material and changing the color in the Inspector window.
- Apply Textures
- To apply a texture, first import an image into the project (e.g., a wood texture for the cube).
- Select the material you want to apply the texture to (CubeMaterial) and in the Inspector, find the
Albedo
field. - Drag your texture onto the Albedo field.
- Apply Materials to GameObjects
- Drag each material onto the corresponding GameObject in the scene.
C# Script for Dynamic Changes
Now, let’s create the C# script that will change the materials and colors of the objects when a button is clicked.
- Create a New C# Script
- Right-click in the Assets folder and select Create > C# Script. Name the script
ChangeMaterials
. - Double-click on the script to open it in Visual Studio.
- Right-click in the Assets folder and select Create > C# Script. Name the script
- Write the Code
using UnityEngine;
using UnityEngine.UI;
public class ChangeMaterials : MonoBehaviour
{
public Button changeButton; // Button to trigger material changes
public Material cubeMaterial, planeMaterial, sphereMaterial;
public Color cubeColor, planeColor, sphereColor;
private Renderer cubeRenderer, planeRenderer, sphereRenderer;
// Start is called before the first frame update
void Start()
{
// Get the renderer components for each object
cubeRenderer = GameObject.Find("Cube").GetComponent<Renderer>();
planeRenderer = GameObject.Find("Plane").GetComponent<Renderer>();
sphereRenderer = GameObject.Find("Sphere").GetComponent<Renderer>();
// Assign the initial materials to the objects
cubeRenderer.material = cubeMaterial;
planeRenderer.material = planeMaterial;
sphereRenderer.material = sphereMaterial;
// Add a listener to the button
changeButton.onClick.AddListener(ChangeObjectMaterials);
}
// Method to change the material and color
void ChangeObjectMaterials()
{
// Change the material and color for the cube
cubeRenderer.material = cubeMaterial;
cubeRenderer.material.color = cubeColor;
// Change the material and color for the plane
planeRenderer.material = planeMaterial;
planeRenderer.material.color = planeColor;
// Change the material and color for the sphere
sphereRenderer.material = sphereMaterial;
sphereRenderer.material.color = sphereColor;
}
}
Explanation of the Code
- Public Variables
Button changeButton
: This is the button in the UI that will trigger the material change when clicked.Material cubeMaterial, planeMaterial, sphereMaterial
: These represent the materials for the Cube, Plane, and Sphere.Color cubeColor, planeColor, sphereColor
: These define the colors that will be applied to each object.
- Private Variables
Renderer cubeRenderer, planeRenderer, sphereRenderer
: These are the renderers for each object that will allow you to access and change the material dynamically.
- Start Method
- The
Start()
method is called when the scene starts. It finds the objects in the scene by their names (Cube
,Plane
,Sphere
) and gets theirRenderer
components. - It then assigns the initial materials to each object.
- The
- ChangeObjectMaterials Method
- This method is called when the button is clicked. It changes the material and color of each object to the specified materials and colors.
Unity Material Customization Cube Plane Sphere 2025
Setting Up the Scene
- Create a Button in the UI
- Go to
GameObject > UI > Button
to create a button in your scene. - Position the button in the scene where you want it.
- Go to
- Link the Script to the Scene
- Drag the
ChangeMaterials
script onto any empty GameObject in the scene (like an empty object called “Controller”). - In the Inspector, drag the
Button
from the scene to thechangeButton
field of the script. - Also, assign the materials and colors for the Cube, Plane, and Sphere in the Inspector.
- Drag the
Testing the Scene
Once everything is set up:
- Press Play in Unity.
- You should see the Cube, Plane, and Sphere with their initial materials and textures.
- When you click the button, the colors and materials of the objects should change dynamically.
Unity Material Customization Cube Plane Sphere 2025 – Conclusion
In this practical, you’ve learned how to:
- Create and apply materials and textures to 3D objects in Unity.
- Write a simple C# script that allows you to change the material and color of objects dynamically when a button is clicked.
- Set up the Unity scene with UI elements like buttons and connect them to C# scripts.
In real-life game development, such interactions are fundamental to creating engaging user experiences. By understanding materials, textures, and user input, you can build more complex scenes and applications.
For AR-VR Notes | Click Here |
For Big Data Analytics (BDA) Notes | Click Here |