Search Unity

TextMesh Pro How to generate your own meshes to mimic TMP objects?

Discussion in 'UGUI & TextMesh Pro' started by Yandalf, Apr 3, 2022.

  1. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Hey everyone!

    I'm working on an ObjectPreview where I want to render an object with TextMeshPro-like labels, but I'm not figuring out how to properly create the meshes required for these labels.
    I'm assuming I have to create a rectangular mesh for each glyph, but creating these with the right dimensions, UVs, and placement from one another is stumping me. I took a quick look at the TextMeshPro source code, but everything there seems to be done with a TextInfo object...
    I need to create only simple 1-3 digit numerical single-line labels, which can probably simplify things.
    Right now my mesh generation code is this, which doesn't give the best results but seems to be mostly a matter of number tweaking and figuring out (kerned) offsets for the different glyph meshes.
    Code (CSharp):
    1.         private Vector2 GetMeshSize(TMP_Character value, out Vector2 minUv, out Vector2 maxUv)
    2.         {
    3.             var glyph = value.glyph;
    4.             var rect = glyph.glyphRect;
    5.             var dimensions = new Vector2(glyph.metrics.width, glyph.metrics.height) / 100f; //Assuming sampling size of 100. This should be read from somewhere?
    6.             minUv = new Vector2(rect.x, rect.y) / 1000f; //Close but wrong, results in approx. single glyph sized UV, but offset wrong
    7.             maxUv = new Vector2(rect.x + rect.width, rect.y + rect.height) / 1000f; //Close but wrong, results in approx. single glyph sized UV, but offset wrong
    8.             return dimensions;
    9.         }
    Any tips, or a way to generate this by relying on existing TMP code I'm unaware of, is greatly appreciated!
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    I'm still looking into this unfortunately. Any help?