x


Change shader property and make all materials using shader change also

Kind of hard to word, but I have a shader and I want to change the float value using another script. When I change the float value of a specific property I want all materials using the shader to change also.

How can I do this? I have looked through the documentation and found nothing.

This is as close as I have gotten, but it only fades the material assigned, not all materials using that shader. In java

snowObject.renderer.sharedMaterial.SetFloat( "_Fade", dynamicSnowFade );
more ▼

asked Aug 09 '12 at 02:32 AM

BHS gravatar image

BHS
206 27 46 53

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
Object [] renderers = GameObject.FindObjectsOfType(typeof(Renderer));
int i_max = renderers.Length;
for (int i = 0; i < i_max; i++)
{
	Material[] materials = ((Renderer)renderers[i]).materials;
	int j_max = materials.Length;
	for (int j = 0; j < j_max; j++)
	{
		if (materials[i].shader.name == "YOUR SHADER")
		{
			materials[i].SetFloat("VALUE NAME", 1.0f);
		}
	}
}
more ▼

answered Aug 09 '12 at 09:19 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

Thank you for this, is it java or C#. I forgot to state that it was in java, my apologies, hopefully it is.

Aug 10 '12 at 02:45 AM BHS

it's C#. i think it is not so hard to translate it to JS if you write in JS 8)

Aug 10 '12 at 11:57 PM ScroodgeM
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1740
x389
x185

asked: Aug 09 '12 at 02:32 AM

Seen: 762 times

Last Updated: Aug 10 '12 at 11:57 PM