Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Individual Letters Sorting Order

Discussion in 'UGUI & TextMesh Pro' started by Ta-da, Jul 18, 2022.

  1. Ta-da

    Ta-da

    Joined:
    Apr 10, 2017
    Posts:
    3
    I'm currently using a raycast, using TMP_TextUtilities, to find the letter that the user is hovering over when they click. That works fine, the problem comes from the fact that some letters seem to overlap others and hide them from the raycast.

    Fish.png

    Is there any way to change the sorting order of the individual letters so that I can bring the hidden letters to the forefront? I've looked and can't seem to find anything, but I'm hoping that someone here knows something I don't. Simply spacing the letters further apart isn't a solution for me unfortunately.
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    Are you sure you are using raycast for figuring which letter was hit or is it just to calculate position to be used for querying TMP_TextUtilities? I am guessing that it is the second . Source code for TMP_TextUtilities.FindIntersectingCharacter is available to you. There is no magic to it, it's just naively iterating over all the characters and returning first one which intersects with asked position. Nothing stops you from creating your on variation which chooses best one by whatever your definition of best is. General solution to this without starting to analyze pixels would be tricky, but if it only needs to work with the the font you are using and limited overlapping complexity you can probably come up with some heuristics that's good enough. Something like choosing last or smallest letter which intersects with position instead of first one might work better with font you showed. In the worst case you could be probably make a table of priorities for different characters or their combinations. Although since font textures already contain distance fields and you would only have to query single pixel for 1-3 characters with intersecting bounding it might not be that bad.
     
    Ta-da and Stephan_B like this.
  3. Ta-da

    Ta-da

    Joined:
    Apr 10, 2017
    Posts:
    3
    I'm using the querying of TMP_TextUtilities, I just assumed it was a raycast given that it needed the mouse position and camera. I didn't know that it just iterated over the characters, thats actually a bit disappointing. I might just have to create a check for problematic letter combinations, like 'f' and 'i', and determine which one is closer to the mouse position.

    Thanks for the Karliss!