Attach Script when using certain shader

I have this custom shader . is there anyway to auto add a script when someone selects this shader for a material on a game object ?

You could probably abuse a custom material editor to do this. Try looking at the Standard shader or the recently released (on the forums) Standard Particle shader.

You could, but removing the script when another shader is selected might be tricky.

I would just create an editor extension that updates the entire scene.

Ahh yeah good point … thanks

I am using as custom material editor but could find no way to add like a script to the object the material is on

The standard particle shader’s GUI.cs does this:

                ParticleSystemRenderer[] renderers = UnityEngine.Object.FindObjectsOfType(typeof(ParticleSystemRenderer)) as ParticleSystemRenderer[];
                foreach (ParticleSystemRenderer renderer in renderers)
                {
                    if (renderer.sharedMaterial == material)
                        renderer.SetActiveVertexStreams(streams);
                }

This is called only when you press a button since it’d be rather expensive. You would have to do something similar, get a list of all Renderers and iterate over the shared material list.