Search Unity

TextMesh Pro Get Sprite/Texture from TMP_SpriteAsset

Discussion in 'UGUI & TextMesh Pro' started by hgrayvivox, Mar 26, 2018.

  1. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    I'm trying to generate an icon picker using TMP. I'd like to be able to retrieve the sprite or texture for a given entry in the TMP_SpriteAsset but they appear to be null in most cases. I've generated my assets using TexturePacker with just PNG/JSON. It seems like overkill to have to convert them into Unity sprite sheet. Is there a way to force TMP to load the sprite and be able to access it at runtime?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Did you use the use the Sprite Importer located in the "Window - TextMeshPro - Sprite Importer" menu? See the following post for more details.

    Are you able to reference these sprites in your sprite asset using the <sprite> tag?

    A Sprite Asset is necessary as it contains the data required to properly layout the sprites along side text. However, the sprite asset only needs a reference to a texture. This texture does not need to be of sprite type and could actually be empty and filled in at run time.
     
  3. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    I did initially. Then I ended up creating Unity textures in TexturePacker and letting their importer convert those to unity sprite sheets. I just noticed that all the unicode values are zero in the TMP sprite assets too so maybe something got borked there. Will retry imports and let you know what I found. Thanks!
     
  4. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    Retried. Now I remember what happened. When I use the TMP SpriteImporter, I get a lot of mismatches in sprite location/rotation and missing data.For example, what it's calling "1f4a7" is showing part of two hands, rotated and cropped. But in the original assets, that sprite is a water drop. The original one included with TMP does show sprites, so maybe there's something wrong with the settings I chose in TexturePacker? sprite_asset_1.png
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Correct. It looks like the UV coordinates for those sprites are wrong. Make sure you are using Json Array in TexturePacker.

    The following post might provide addition info as well.
     
  6. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    Yeah, I am. I was just going to add that to the previous post. However, even with that wackiness, the sprite property of each TMP_Sprite in the sprite asset's sprint info list has a null sprite. Even the ones which look to line up correctly with their coords.
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you provide an example of the code you are using? This would make it easier for me to provide you with additional insight.
     
    Last edited: Mar 27, 2018
  8. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    I'm trying to access the sprites so I can create my own picker. I can load the assets on my own, but I figured if TMP already had them, I didn't have to find which sprite sheet they live in and load them using resource load.

    I went back to using the TexturePacker Unity export, and then TP's importer to generate Unity sprite sheets. Then use those for TMP. TMP coordinates now look correct, but they all have a zero unicode, so they don't actually map to unicode within the text.
     
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    This automatic Unicode mapping comes from using the names of the sprites from the json array (assuming the names of the sprites is their unicode value) and this is only possible when using the TMP Sprite Importer.
     
  10. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    Damn! So I'm stuck here. I've tried a bunch of different settings in TP but every combo I've tried (mostly default, or slight variations on default) causes a mismatch in coordinates in TMP. If I do the TP->Unity->TMP route, coordinates are fine but no unicode.

    Oh, maybe I could run a post-process to assign the unicode by file name. What's the special sauce for generating your unicode int from those multi-hypenated file names?

    This is all in an effort to add a bunch of the EmojiOne sprites, some of which have 3 or 4 part names. Thanks again!
     
  11. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I just tried Texture Packer version 4.6.3 with the following configuration and everything worked as expected when I used the resulting file with the TMP Sprite Importer.

    upload_2018-3-26_21-2-18.png

    upload_2018-3-26_21-2-57.png
     
  12. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    Same version. Hmmm. Maybe the issue comes in with larger sets? I'm pulling in the entire 2666 EmojiOne set. The first batch look pretty good, maybe it's the multi pack stuff?
     
  13. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Try testing on a smaller set just to see if that works. Also be mindful of Unity potentially compressing the textures which would affect the UV coordinates.
     
  14. hgrayvivox

    hgrayvivox

    Joined:
    Mar 3, 2017
    Posts:
    15
    Thanks, sorry I've been tied up with other stuff. I haven't been able to try different things to find where it breaks. I'll let you know what I find.