- usingSystem.Collections.Generic;
- {
- MaterialPropertyBlock props;
- Renderer[] renderers;
- //Animator anim; uncomment if you have an animator attached and a hit animation
- void Start()
- props =new MaterialPropertyBlock();
- renderers = transform.GetComponentsInChildren<Renderer>();
- // anim = GetComponent<Animator>(); uncomment if you have an animator attached and a hit animation
- }
- // Update is called once per frame
- {
- {
- // anim.Play('Hit'); uncomment if you have an animator attached and a hit animation
- }
- {
- for(int i =0; i < renderers.Length; i++)
- renderers[i].SetPropertyBlock(props);
- yieldreturnnew WaitForSeconds(0.1f);
- for(int i =0; i < renderers.Length; i++)
- renderers[i].SetPropertyBlock(props);
- }
It allows you to easily shove meshes to the GPU, customize them with MaterialPropertyBlock s, and avoid the fat overhead of GameObject s. Additionally, Unity has to do a little less work to figure out if it can instance the objects or not, and will throw an error rather than silently nerfing performance. To override the material's color, we have to provide the object's renderer component with a material property block. Do that by creating a new MaterialPropertyBlock object instance, give it a Color property via its SetColor method, then pass it to the object's MeshRenderer component, by invoking its SetPropertyBlock method. MaterialPropertyBlock is used by Graphics.DrawMesh and Renderer.SetPropertyBlock. Use it in situations where you want to draw multiple objects with the same material, but slightly different properties. For example, if you want to slightly change the color of each mesh drawn. Changing the render state is not supported. You can do this with a MaterialPropertyBlock, just like you would in the old pipeline to reduce the overhead of instantiating modified copies of materials. The only difference is that the shader property is called 'BaseColor' not 'Color'.