unity3d - Unity FBX problems


Keywords:unity3d 


Question: 

I'd like to make whale's color different.

one would rendered this skin, and other would be another one.

but when I try to edit color, two of them's colors are changed concurrently.

their skin follow last one.

what should I do?

enter image description here


2 Answers: 

In order to optimize rendering, when two identical objects are using the same material or mesh, it is actually the same instance that is used. Simply, your two whales are using the same material. This is to save draw calls.

So when you change it, it changes for both.

The solution is to duplicate the material and assign it to one of them. Now you can change one without affecting the other. You also added one extra draw call.

 

As an alternative solution to duplicating the material (which prevents both whales from batching!) you can use MaterialPropertyBlocks.

They're generally pretty easy to use. In a new script, you create a MaterialPropertyBlock, set the color to the color you want the object to have, and assign the MaterialPropertyBlockto the object's renderer.

MaterialPropertyBlock.SetColor() has a working example, although the Draw calls aren't needed (it's an over-engineered example). This is sufficient:

Start() {
    MaterialPropertyBlock mpb = new MaterialPropertyBlock();
    mpb.SetColor("_MainColor", new Color(1,0,0));
    gameObject.GetComponent<MeshRenderer>().SetPropertyBlock(mpb);
}

Results:

Only one material

This ends up being more efficient in the long term, as you can do some very complex things with shaders, have every object use the same single material, but all of them have their own unique look, and rendering speed doesn't suffer. The hard part is figuring out the names of the properties in the shader that you want to tweak.

And of course, the results only show up when it's run in Play Mode (unless you tag the class with [ExecuteInEditMode]).