Search Unity

Question Edit shader properties for multi-mesh objects

Discussion in 'Shaders' started by ScottySR, Jul 19, 2021.

  1. ScottySR

    ScottySR

    Joined:
    Sep 29, 2018
    Posts:
    49
    I'm running into a problem where I have objects with multiple meshes and I need to change some shader properties for visual effects. The problem is that all objects that use the same material will do that effect too. Creating new instance in object initialization doesn't seem to work.

    I do know of property blocks, but it seems like they only affect one mesh renderer. Do I have to just foreach for all renderers in that object (is it slow?) or is there a way to group multiple renderers so all of them change in the same object while leaving other objects unaffected?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    If you need to change multiple renderers in a single game object without affecting others ... then yes you have to get a list of all of the child renderer components. That's going to be the case regardless of if you're modifying the materials directly or using material property blocks.

    The "best" way to do that would be to have a custom script component on your object somewhere that already has the list of renderers you care about. Then you can
    foreach
    over all of them and set their property block. And no, that's not really very expensive.

    The "easier" way (less prep) would be to use
    GetComponentsInChildren<Renderer>()
    to get an array of all the child renderer components, which you could cache and
    foreach
    over.