Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

TextMesh Pro Material properties per renderer.

Discussion in 'UGUI & TextMesh Pro' started by Pharan, Jul 4, 2017.

  1. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    92
    I'm trying to get some functionality similar to MaterialPropertyBlock but for TextMeshProUGUI and UnityEngine.UI CanvasRenderer stuff in general.
    It seems like there's nowhere to get it working.

    So for TextMeshPro, I'm just cloning the material at runtime so it's not shared, and then modifying it.
    Code (csharp):
    1. TextMeshProUGUI text;
    2. Material material;
    3.  
    4. public void SetDilate (float dilateValue)
    5. {
    6.     if (material == null)
    7.     {
    8.         material = new Material(text.fontSharedMaterial);
    9.         text.fontMaterial = material;
    10.     }
    11.  
    12.     material.SetFloat(ShaderUtilities.ID_FaceDilate, dilateValue);
    13. }
    I'm doing this so I can animate the material properties per Renderer through code, the same reason why I would have used MaterialPropertyBlock.

    Is there no better way to do this? Does MaterialPropertyBlock not work at all with these components?

    'cause the above is arguably the dumbed-down version for readability. I still have to check every time if the material on the renderer is the material I currently have.
    I know this is not quite TextMeshPro's fault. The functionality seems missing from CanvasRenderer altogether with no clear alternative for it(?).
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,676
    MaterialPropertyBlocks do not work with the CanvasRenderer (sadly). So you have to instantiate your own material and then modify the properties manually.
     
unityunity