Scripting Material Parameters

Similar to just about everything else in Unity, materials include a scripting API so that you can change their properties while the game is running. This can happen instantaneously, or you can animate these properties over time.

Unity Documentation

Material - This section of the Unity documentation details the Material class, which exposes all properties of a material.

Renderer.material - The Renderer.material property returns the first instantiated Material assigned to the renderer. You should generally only modify materials using this property, because it will automatically clone the material so that scripting changes don't impact other renderers that share the same material.

Resources

Materials in Unity - Project Files - This is a zip file that contains the Unity project for this course. Download the file, unzip it, and then in your file browser (Finder on OS X and Exporer on Windows) navigate to Materials in Unity > Assets > _Scenes. Finally, choose the scene you'd like to open.

Attribution

Shader Ball - The "Shader Ball" mesh used in these videos is made available by Allegorithmic's Substance Share service and is licensed under Creative Common's "Attribution 4.0 International" listed in Appendix 1 of the Substance Share EULA.

0:00

[MUSIC]

0:04

Similar to just about everything else in
Unity, materials include a scripting API,

0:09

so that you can change their
properties while the game is running.

0:14

This can happen instantaneously, or you
can animate these properties over time.

0:20

However, modifying materials is a bit
different from modifying other components,

0:25

let's take a look.

0:27

For this example,
we're going to focus on the lava material.

0:32

I'm going to select the lava
shader ball In scene view and

0:38

then double-click its material
in the Inspector and this

0:44

material would be a lot more believable
and interesting if the lava was moving.

0:51

In the Inspector under
the main maps section,

0:55

there's an X and a Y field called Offset.

1:01

This changes how the material
is mapped to the 3D model.

1:05

And it determine where it should start and
where it should end.

1:10

I'm going to adjust the Y
value as slowly as I can.

1:16

And you can see how the material
slides along the surface.

1:23

I'm going to undo those changes for now.

1:25

The effect isn't perfect but
if it were animated smoothly and

1:29

updated every frame,
it should look better.

1:33

Later on, we're going to add
a script to the lava shader ball and

1:38

animate its materials, but
before we start coding,

1:41

let's look at the unity documentation
to see how this might be accomplished.

1:46

Here, I have the documentation for
the material class.

1:50

You can find a link to this page in
the notes associated with this video.

1:55

In the description there's a very
important note about materials.

2:01

It says, in order to get
the material used by an object,

2:06

use the renderer.material property.

2:10

We want to modify the materials
on our game objects, so

2:13

we'll want to do what this note says.

2:15

Click the renderer.material link for
more information.

2:21

Here it says that the material
property on the renderer class

2:28

will return an object of type material,
then it also says that it

2:33

automatically extantiates the materials
and makes them unique to this renderer.

2:39

That's pretty important to understand,

2:41

so let's switch back to Unity and
break it down.

2:44

If I click the lava shader
ball in scene view,

2:49

in the inspector, you can see that
it has a mesh renderer component.

2:56

This is how unity knows that this
game object should actually be shown

3:00

in the game view and
scene view as a 3D object.

3:04

The mesh renderer is just one
type of renderer component, but

3:09

it's the most commonly used renderer
because it's for 3D objects.

3:14

So when we make that method
call to renderer.material we're

3:21

actually getting this renderer
component and not material.

3:28

Why might that be?

3:29

Why not just grab this material directly?

3:32

After all when there's a material in
the mesh render element zero slot

3:38

under the material section it too
looks like its added as a component so

3:43

we should be able to just
grab this component, right?

3:48

Materials are one of the few
objects in Unity that won't

3:51

revert to their previous settings if they
get changed while the game is running.

3:57

In other words, if you run the game and

3:59

then change material settings
in the inspector, those changes

4:04

won't go back to their original
settings before the game was running.

4:09

That's also true in builds.

4:11

If you write a script that
changes a material directly,

4:15

any objects in the game that also use that
same material will be modified, as well.

4:22

This is different than how most
components behave in unity, and

4:26

in short it's because materials can be
shared across many different game objects.

4:32

Let me quickly demonstrate.

4:34

With the lava shader ball selected,
if I click the play button.

4:41

Then in the inspector I adjust
the emissive intensity so

4:46

that it's extremely bright.

4:49

When I click the play button
again to stop the game,

4:53

it won't go back to
it's original settings.

4:56

For now, I'm going to set this back
to it's original value of six.

5:02

Because some of the reflections
are being rendered in real

5:05

time in this particular scene,
you may need to hit the play button again

5:10

to get them to revert back
to normal completely.

5:13

So with all of these in mind, that our
material does not retained its properties.

5:19

Let's go back to that same piece of
documentation and re-read it again.

5:25

I'll hit the back button to go back to the
material page and it says in order to get

5:30

the material use by an object use
the Rendered.material property.

5:36

And then here it says if the material
is used by any other renderers,

5:42

this will clone the shared material and
start using it from now on.

5:48

This is exactly what we want to happen.

5:50

When we modify a material through
the renderer on a match it will create

5:56

a duplicate of that material so that we
don't change its values permanently or

6:01

mess up any models that are also
using the same material.

6:06

Now that you understand how materials
are different from other components,