MINI TUTORIAL: SIMPLE MATERIAL SWITCHER
Using Unity Version 2019.4
There are a million reasons you might want to change a material in runtime. This is a really easy tutorial that will show you how. Better yet, it can be easily extended!
The (Example) Problem
Your character is wearing an outfit with a material you want to change. You have a new material ready to go, but you don't know how to swap them.
The solution for this problem is simple: Create a material-switching code that will take care of the problem!
Step One: Setup
Begin by creating a duplicate material for each material in the mesh renderer that you want to change. I’m using Abigail from my Ultimate Stylized Business Women asset. The prefab I’ve chosen is Outfit 1, which contains several mesh pieces and multiple renderers. For this tutorial, the array index and renderer don't matter. I just want to change the shirt, so I’ll create a duplicate of the blouse material. NOTE: The reason you create a duplicate is because this code only works if the material you are replacing uses the same shader as the material you replaced.
Rename the duplicate to indicate it is your “live material” (I’ve called it “Blouse_Live”).
Replace the old material with this one. In my mesh, there are several materials. Just make sure you put it in the same index as the old one.
Repeat this step for each material you want to be able to change.
Step Two: The Code
Create a new C# script. I called mine “MaterialSwitcher”. Because this method doesn’t actually use the mesh renderer to return the material index, you can put this code on any active object in the scene. I’ve created an empty gameObject called “Material Manager”.
Here’s the code:
So how does it work?
First, you'll notice we don’t need very many parameters for the code – just a few material references.
Material liveMat: This is the live material we created and applied to our mesh renderer.
Material newMats: This is a material array which will hold all of the material options which we can swap with our live material options. Optional:
Material defaultMat: This is the material we always want to be used at the start. Any other material we are using will be overwritten with these material parameters.
We aren’t actually changing the material in the mesh renderer. What we’re doing is copying the parameters from another material over to the live material. The function works by taking an integer parameter, which will tell us which material in our newMats array to use. The it takes the properties (textures, settings, etc.) from the material in the array and copies them to the live material.
To use the code, populate the fields accordingly. Here's how mine looks:
TIP: You can populate the entire array by locking the inspector, highlighting all of the materials, and dragging them onto the parameter.
Step Three: Test
For the sake of this tutorial, I’ve created a simple grid of buttons (using a grid layout element) for different colors. Obviously you don’t have to do it this way, but it’s just the easiest way to show the functionality.
For each button, I've set the color to match a material. If you drag and drop your Material Switcher object to the onClick method, you should be able to select the MaterialSwitcher ChangeLiveMat() function. Then simply pass the corresponding integer from the newMats array.
And voila! Here's how it works.
Used in this article: