Animation — MRTK3
Most properties on the Graphics Tools Standard shader can be animated using Unity's built-in animation system. Materials that are used on Unity UI components don't expose their material properties to Unity's animation system by default (nor do they support material property blocks). Graphics Tools contains a system to support the animation of Unity UI material properties.
The CanvasMaterialAnimatorGraphicsToolsStandardCanvas.cs script exposes all material properties available in the Graphics Tools/Standard Canvas shader. Adding this component to a UnityUI game object with a CanvasRenderer will expose material properties to Unity's animation system and will automatically update the correct material when animated.
Note
CanvasMaterialAnimatorGraphicsToolsStandardCanvas.cs only works with the Graphics Tools/Standard Canvas shader. For other shaders, use their corresponding animation script. For example, CanvasMaterialAnimatorCanvasBackplate.cs for the Graphics Tools/Canvas/Backplate
shader.
Programmatic usage
Normally a canvas material animator is driven by Unity's animation system, however, it's possible to use this class programmatically. After changing any of the class's members, make sure to call the ApplyToMaterial
method. An example of pulsing the vertex extrusion amount is below:
using UnityEngine;
public class ScriptedMaterialAnimation : MonoBehaviour
{
public CanvasMaterialAnimatorGraphicsToolsStandardCanvas Animator;
private void Update()
{
Animator._VertexExtrusionValue = Mathf.Lerp(0, 0.002f, (Mathf.Sin(Mathf.Repeat(Time.time, Mathf.PI * 2.0f)) + 1.0f) * 0.5f);
Animator.ApplyToMaterial();
}
}
Advanced usage
If you inspect the content of CanvasMaterialAnimatorGraphicsToolsStandardCanvas.cs, there's boilerplate code that could fall out of sync with the Graphics Tools/Standard Canvas shader. Fortunately, this code is auto generated by right clicking on a shader in the project window and selecting Graphics Tools > Generate Canvas Material Animator.
You can generate a canvas material animator for any shader your project needs to animate. Material properties will be updated at edit and run time.
Note
By default, canvas material animators operate on the renderer's shared material. If you would like the animation to only affect a single material, you can select the Instance Materials property on the canvas material animator's inspector. This will allocate a new material for every instance.
It's also worth noting that when animating shared materials at run time in the editor material updates may get serialized to disk. To avoid this, Graphics Tools uses the MaterialRestorer.cs pattern.