Skip to content

Instantly share code, notes, and snippets.

@Chaosed0
Last active April 17, 2019 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chaosed0/258ba61255a27c42736566f1593293ca to your computer and use it in GitHub Desktop.
Save Chaosed0/258ba61255a27c42736566f1593293ca to your computer and use it in GitHub Desktop.
Component to pass color to shader through property
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class ModifyColor : MonoBehaviour
{
private static MaterialPropertyBlock materialPropertyBlock = null;
[SerializeField]
private string propertyName = "_Color";
[SerializeField]
private Color color = Color.white;
[SerializeField]
private Renderer renderer = null;
private void LateUpdate()
{
if (materialPropertyBlock == null)
{
materialPropertyBlock = new MaterialPropertyBlock();
}
if (renderer != null)
{
for (int i = 0; i < renderer.sharedMaterials.Length; i++)
{
renderer.GetPropertyBlock(materialPropertyBlock, i);
materialPropertyBlock.SetColor(propertyName, color);
renderer.SetPropertyBlock(materialPropertyBlock, i);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment