Search Unity

TextMesh Pro Detecting mouse click on sprite

Discussion in 'UGUI & TextMesh Pro' started by Eggpunk, Mar 28, 2018.

  1. Eggpunk

    Eggpunk

    Joined:
    Nov 2, 2014
    Posts:
    39
    I have been looking over the documentation, threads, and examples for TMP to learn how to detect a mouse click on a TMP sprite. In example 12a I replaced the first word "See" with "<sprite=0>".

    The console showed: "Character [] at Index: 0 has been selected."

    Given the above, I am not seeing how a user might script sprite A to become sprite B on mouse click.

    Has anyone here had experience with these interactions? Am I missing something, or is this perhaps not a current feature?

    Any guidance would be greatly appreciated.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I concur this is confusing at first. So what does it actually mean?

    We know that we have some character that cannot be displayed [] at Index: 0. The important part here is at Index 0.

    Whenever a text object is generated, the TMP_Text.textInfo data structure gets populated with all kinds of information about the text object. This textInfo contains several structures like characterInfo, wordInfo, lineInfo, etc. and even an array of meshInfo which contains the geometry of it all.

    So if you were to check the content of this TMP_Text.textInfo.characterInfo[0] which is the index we got initially, we could check the text elementType which would indicate it is a sprite. We could also check the spriteIndex. We could also check the materialIndex which would allow us to know in which mesh it is located and even what material it is using.

    Changing the sprite is trickier since the sprite is defined by a <sprite> tag. So although we know this sprite is the first text element, you could do some string replace of the tag.

    Alternatively and most likely easier to handle would be to assign a Unicode value to each of your sprites. For instance you could use E000 for the first and E001 for second, etc. This E000 is defined as private use area by the Unicode.

    Then instead of having to replace a <sprite> tag, you would reference the sprite in your text using the UTF16 value which in this case would be "\uE000". Then your string replace would be for a single character like "\uE001".

    Now back to the reporting of character [] which didn't mean much before, if you were to check the value of this char (which didn't print but still had a value), you would see that its value is "E000" or whatever Unicode value you have assigned.

    BTW: Emojis are typically mapped in the UTF32 Unicode range like \U0001F600 which is the smiley face.
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Made a change so that in the next release, you will actually be able to differentiate between a character and sprite.

    upload_2018-3-28_1-14-49.png
     
    Carychao, Eggpunk and Beks_Omega like this.
  4. Eggpunk

    Eggpunk

    Joined:
    Nov 2, 2014
    Posts:
    39
    Sorry for the long delay in response Stephan after your super quick reply. This helped a lot.
    Looking forward to the next release!