This function takes in a string value, which I specify as “_Color”. The main thing to pay attention to is the material.SetColor() function. Yield return null // wait for a frame then loop again deltaTime / transitionTime // Increment transitionRate over the length of transitionTime We Lerp between the current color and newColor this next line is how we change our material color property. * until transitionRate is incremented to 1 or higher * 1 is the highest value that the Color.Lerp function uses for This is necessary for our next while loop to function Infinite loop will ensure our coroutine runs all gameĬolor newColor = colors // Assign newColor to a random color from our arrayįloat transitionRate = 0 //Create and set transitionRate to 0. start our coroutine when the game starts initialize our array indexes with colors ThisRend = GetComponent () // grab the renderer component on our cubeĬolors = new Color // We will randomize through this array Renderer thisRend //Renderer of our Cubeįloat transitionTime = 5f // Amount of time it takes to fade between colors I have done my best to use comments to explain what I’m doing in the script: Use this Code: Open the script up in your IDE of choice and copy the below code into the script. Type “MatScript” in choose to create a new C# script. Let’s Get Coding!Ĭlick on the Cube in your hierarchy and click “Add Component”. This is the easiest way to find out the name of the property that you want to modify via code. When you work on your own Unity project, you can always use the above methodology to find the shader properties of the shader your material is using. What we will do is call on the _Color property and use a coroutine to change the color over time. For example, “_Color” is a property name that we can call on to get the color property of the material. The information we are most concerned with are the shader properties, seen here:Įach of these properties can be called on via code by their property name. Click the gear icon at the top-right of the inspector and choose “Select Shader”, as seen here:Īt this point, the inspector will show you all sorts of information about the shader that your material is using. Go to the asset viewer and left click on your material that you created. Select the Cube in the hierarchy and replace the material with the new material.Ĥ. Right-click on your asset viewer and choose Create → Name it whatever you want.ģ. So, first thing’s first, create an empty 3D project. This is a slightly advanced topic, so I am going to assume you have a working knowledge of Unity’s basics. How to Change Material Properties in Unity The goal is to end up with is a 3D cube that changes color over time. So, in order to make life easier for you, this post explains how you can manipulate material properties using C# scripts in Unity. However, Unity’s API was not as simple for this task as I had hoped. I needed to be able to use C# scripts to make the material change features during gameplay. I did not want a static, unchanging material. Recently, I wanted to have the ability to dynamically change the material properties of objects when the player looked at them. I am working on a Unity Project in my free time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |