Animate[Animation API] a shared material on a character?

Hi,

I would like to know that if there a way to animate the shared material on a character that there are many node and each node also have the same material by using Unity Animation API?

I search the forum and saw only a way to change through code by using renderer.sharedmaterail but how about through Animation API , is that possible?

Thank you,

I’m not sure if what you want to do can be done using the animation editor but it may well be possible from a script. Can you give any more detail about what kind of animation you need?

Hello broccolizz,

I was animating an objects material using the animation user interface. Whilst I was happy with the visual output I was getting performance spikes where the animation clip was internally being processed when objects are activated/deactivated.

So I decided to animate the shared material instead, I still used the animation editor however. Here is how I achieved it:

  • Created a cube and assigned the material to the cube renderer.
  • Place cube some where convenient and disable the mesh renderer component so that it will not be seen.
  • Create and assign a script to the cube. The scripts job is simply to copy the animated property value from the renderer material into the sharedmaterial and thus causing all other instances of material to animate.
  • Be sure that “Always Animate” is selected under animation clip culling.

The following script will retrieve all properties from animated material and apply them to shared material:

using UnityEngine;
using System.Collections;

public class AnimateSharedMaterial : MonoBehaviour {

	Material _material;
	Material _sharedMaterial;
	
	void Start() {
		_sharedMaterial = renderer.sharedMaterial;
		_material = renderer.material;
	}
	
	void Update() {
		_sharedMaterial.CopyPropertiesFromMaterial(_material);
	}

}

I hope this will be of use to you.

Nice trick!

In fact a cube, or a MeshFilter component isn’t necessary, just MeshRenderer is enough for this to work.

This doesn’t seem to work in Unity Editor 5.0.2f1. I have a simple animation clip that loops, pulsing the Mesh Renderer.Material._Color. I see the color pulse in game on the object that has an Animator with the clip looping, but the color property of the material that I am attempting to copy the properties from is always white, and so the shared material and the objects sharing it are not affected.

created “(Case 698923) Material properties animated by animation clips can’t be shared with other objects”

thanks numberKruncher

actually it doesnt work. when i am running the game and i change emission or the color of the material through the inspector, then the change is applied to all the objects sharing the material. but when i change the emission property through the animator, the changes wont apply. i have tried both material._Emmission Color and material._Emmission Color UI.

I recently heard from support that you now need to copy the MaterialPropertyBlock from each Renderer animated in this way to share the animation in Unity 5. I haven’t tried it yet.

For the record, i tried this out and using the material property blocks seems to work. Here’s the script, working in 2019.1

using UnityEngine;

[RequireComponent(typeof(Renderer))]
public class MaterialCopy : MonoBehaviour {

    private Renderer slave;
    public Renderer master;
    private MaterialPropertyBlock block;

    private void Start() {
        slave = GetComponent<Renderer>();
        block = new MaterialPropertyBlock();
    }

    private void Update() {
        int n = master.materials.Length;
        master.GetPropertyBlock(block);
        for(int i = 0; i < n; i++)
            slave.SetPropertyBlock(block);
    }
}