Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feedback Image.material sharing material by default

Discussion in 'UGUI & TextMesh Pro' started by Sinister-Design, Jan 26, 2021.

  1. Sinister-Design

    Sinister-Design

    Joined:
    Sep 19, 2015
    Posts:
    65
    The issues described in this 7-year-old thread are still present in Unity today: Image.material behaves like renderer.sharedMaterial, forcing changes to every instance of an image whenever one Image instance's material is edited. Using Image.GetComponent<CanvasRenderer>().GetMaterial() and Image.GetComponent<CanvasRenderer>().SetMaterial() produces the same issue.

    In order to get around this issue, I had to resort to code like this:

    Code (CSharp):
    1.  
    2.         material = new Material( image.material );
    3.  
    4.        //code making changes to the newly instanced material
    5.  
    6.         image.GetComponent<CanvasRenderer>().SetMaterial( material , 0 );
    This took a while to puzzle out. For the sake of a better user experience, I suggest surfacing a separate shared material as a sharedMaterial property and making material an instanced material the user can edit directly, as in the Renderer component--or, in the alternative, making GetMaterial() in CanvasRenderer return an instanced material.
     
  2. Sinister-Design

    Sinister-Design

    Joined:
    Sep 19, 2015
    Posts:
    65
    ...well, here's a fun new one: apparently a CanvasRenderer can have a materialCount of 0, making it impossible to CanvasRenderer.SetMaterial() without throwing a range error on the second parameter. I'm adding "fix that" to my original feedback.