Last active
April 17, 2019 06:17
-
-
Save Chaosed0/258ba61255a27c42736566f1593293ca to your computer and use it in GitHub Desktop.
Component to pass color to shader through property
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; | |
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