- Home /
After animating a custom material/shader parameter, how do I get an accurate value from script?
For example, I create a new shader with a Color parameter, then create a new material that uses that shader.
I assign that material to a new 3D object, and use an Animator (on another object) to animate that object's material's Color values. Hitting Play, I can see the object changing color correctly.
However, I have a component on the object where I just want to query the current animated color from the material. e.g.
void Update {
Debug.Log(GetComponent().material.GetColor("_Colour"));
Debug.Log(renderer.material.color);
Debug.Log(GetComponent().sharedMaterial.GetColor("_Colour"));
Debug.Log(renderer.sharedMaterial.color);
}
All of these only output the value that the Color attribute was BEFORE playing, not while the animation is live. How can I get the live animated value?
Same problem here. I can't find a way to get the actual color of that material, which gets correctly displayed by the editor.
Answer by hicks · Jan 28, 2016 at 05:38 PM
Just found an answer! I noticed a message in the inspector saying that a MaterialPropertyBlock is used to control the values. I guess the animator does that during playmode.
Reading from the property block solves this issue:
MaterialPropertyBlock properties = new MaterialPropertyBlock();
renderer.GetPropertyBlock(properties);
Vector4 color = properties.GetVector("_Color");
Debug.Log("color: " + color);
Answer by lassade · Jan 28, 2016 at 01:40 PM
Try use the LateUpdate event, it works the same way but gets called after the animations.
Check this out http://docs.unity3d.com/Manual/ExecutionOrder.html
Answer by LTPStudioXR · Mar 26, 2018 at 06:08 PM
Been wracking my brain trying to figure this out and scouring forums for this. Was trying to get the animation > animation.clip > animation.clip.property(curves, component, etc) so Thank You!