Search Unity

Attach Script when using certain shader

Discussion in 'Shaders' started by echologin, Jun 30, 2017.

  1. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    1,078
    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 ?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,341
    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.
     
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    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.
     
  4. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    1,078
    Ahh yeah good point ... thanks
     
  5. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    1,078

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

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,341
    The standard particle shader's GUI.cs does this:
    Code (CSharp):
    1.                 ParticleSystemRenderer[] renderers = UnityEngine.Object.FindObjectsOfType(typeof(ParticleSystemRenderer)) as ParticleSystemRenderer[];
    2.                 foreach (ParticleSystemRenderer renderer in renderers)
    3.                 {
    4.                     if (renderer.sharedMaterial == material)
    5.                         renderer.SetActiveVertexStreams(streams);
    6.                 }
    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.