Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
24 lines (21 sloc)
819 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class ColorPropertySetter : MonoBehaviour | |
{ | |
//The color of the object | |
public Color MaterialColor; | |
//The material property block we pass to the GPU | |
private MaterialPropertyBlock propertyBlock; | |
// OnValidate is called in the editor after the component is edited | |
void OnValidate() | |
{ | |
//create propertyblock only if none exists | |
if (propertyBlock == null) | |
propertyBlock = new MaterialPropertyBlock(); | |
//Get a renderer component either of the own gameobject or of a child | |
Renderer renderer = GetComponentInChildren<Renderer>(); | |
//set the color property | |
propertyBlock.SetColor("_Color", MaterialColor); | |
//apply propertyBlock to renderer | |
renderer.SetPropertyBlock(propertyBlock); | |
} | |
} |