Search Unity

CanvasRenderer crashes Unity when given a mesh that has submeshes & more than one material.

Discussion in 'UGUI & TextMesh Pro' started by KaiClavier, Nov 2, 2016.

  1. KaiClavier

    KaiClavier

    Joined:
    Jan 23, 2015
    Posts:
    70
    I'm running into a very inconsistent crash when using the canvas renderer to render a custom text component.

    In my text component, I normally just render using a meshfilter and a meshrenderer. Setting meshfilter.sharedMesh to be the generated mesh and meshrenderer.sharedMaterials to be the generated materials works perfect, but I run into problems when using a canvasrenderer instead. (Within the script, the code sees if it has a transform or a recttransform component and sets a bool named "uiMode" based on that, so it knows which renderer to use.)

    Here's some sample code:
    Code (CSharp):
    1.  
    2. Mesh textMesh; //assume this is a mesh that's had its vertices, triangles, etc already set. Has submeshes for every material to be attached, if there's going to be more than one material needed.
    3. //send generated mesh to the meshfilter/canvasrenderer:
    4. if(uiMode){ //Send to canvas renderer
    5.     canvasRenderer.SetMesh(textMesh);
    6. }else{ //send to mesh filter
    7.     meshFilter.sharedMesh = textMesh;
    8. }
    9. //Set Materials
    10. ClearMaterials(); //A function that clears materials attached to the canvasrenderer or meshfilter
    11. Material[] newMats; //assume this is an already-filled array that has the needed materials
    12. if(uiMode){ //set canvasrenderer materials
    13.        canvasRenderer.materialCount = newMats.Length;
    14.        for(int j=0, jL=newMats.Length; j<jL; j++){
    15.            canvasRenderer.SetMaterial(newMats[j],j); //set material one-by-one
    16.        }
    17. }else{ //set meshrenderer materials
    18.     meshRenderer.sharedMaterials = newMats; //set array directly
    19. }
    20.  
    If UI mode is true, this causes a crash. But here's the strange part: Sometimes it causes an immediate crash when the mesh has more than one material, but sometimes for a split second it works fine. It renders the mesh exactly as I'd expect... then crashes a few seconds after. Once I even got it to go into play mode, but then upon exiting it crashed immediately.

    Replacing the code under "//Set Materials" above with this, never causes a crash:
    Code (CSharp):
    1.  
    2. //Set Materials
    3. ClearMaterials(); //A function that clears materials attached to the canvasrenderer or meshfilter
    4. Material[] newMats; //assume this is an already-filled array that has the needed materials
    5. if(uiMode){ //set canvasrenderer materials
    6.    canvasRenderer.materialCount = 1;
    7.    canvasRenderer.SetMaterial(newMats[0],0); //only apply first material
    8. }else{ //set meshrenderer materials
    9.     meshRenderer.sharedMaterials = newMats; //set array directly
    10. }
    11.  
    So if I set the mesh to only have the first material on it, it doesn't crash, and just renders nothing for the characters that would have used the other materials.

    Does anyone have any clues what might be causing the crash? Am I using the canvasrenderer wrong, or does Unity have a bug with the canvasrenderer and submeshes w/ multiple materials? What can make a canvasrenderer crash unity instead of returning an error in the console?
     
  2. KaiClavier

    KaiClavier

    Joined:
    Jan 23, 2015
    Posts:
    70
    Anyone got any ideas? Still not sure if I'm using the canvasrenderer wrong or if it's a weirdly-specific bug.
     
  3. KaiClavier

    KaiClavier

    Joined:
    Jan 23, 2015
    Posts:
    70
    It's been another 2 months, is there anyone who knows anything about this?
     
  4. KaiClavier

    KaiClavier

    Joined:
    Jan 23, 2015
    Posts:
    70
    The solution came to me in a dream: setting the material count to be the materials array's length + 1, but having it iterate over just the length of the materials array seems to not horrifically crash Unity.
     
  5. KaiClavier

    KaiClavier

    Joined:
    Jan 23, 2015
    Posts:
    70