Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Set renderer.material without creating a new material

Discussion in 'Scripting' started by DiamondWolf2, Apr 11, 2020.

  1. DiamondWolf2

    DiamondWolf2

    Joined:
    Dec 9, 2018
    Posts:
    16
    I am working on a project which loads user-supplied levels on the fly. Part of this process includes loading an image from the hard drive, creating a material from that image, and applying it to whatever geometry needs it. This process results in a lot of lag (several seconds per frame) if I have a lot of geometry, however. Between the docs and some Googling, I gathered that setting a renderer's material creates a brand new copy of the material, which means it cannot be batched with other geometry that uses the exact same material. Is there a way to get around this? If not, is there a way I can at least make things playable?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    You should be able to use the .sharedMaterial property instead, but it still might require subtle changes in how you instantiate your materials. At least it's something more you can experiment with.