Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Text characters forced to pixel-grid int positions?

Discussion in 'UGUI & TextMesh Pro' started by Zebbi, Dec 20, 2018.

  1. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    I'm using a monospace font with the pixel-perfect package, at the correct size so the height of each character fills 8 "pixels" correctly and for the main part it looks fine, except occasionally it will get offset, or it will be positions on a float and the characters get slightly scrambled. I have a script that forces the text block to int positions, but it really needs to work on a per-character basis. Is this possible with textmesh pro?
     
  2. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Is this something textmesh would be capable of?
     
  3. TextMeshPro is generating mesh, it's not pixel perfect AFAIK.
     
  4. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Can can characters be forced to int/non-float positions?
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Similar to how you can access the resulting geometry of the text object to animated the vertices positions to do things like Jitter, etc. You could implement your own handling / adjustment of character positions to always be pixel aligned.

    Take a look at the example 23 - Animating Vertex Attributes and relevant scripts included in the TMP Examples & Extras. This should help you get a better understanding of how to modify the vertices position to achieve what you seek.
     
    Zebbi and Lurking-Ninja like this.
  6. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Thanks, I've had a look at the example but I'm a bit of a beginner and could use a bit more of a practical example of how to employ this! For instance, this is the script I'm using to lock the actual text GameObject to int positions:
    Code (CSharp):
    1. private Text text;
    2.     private Vector2 originalPosition;
    3.     private string lastText;
    4.  
    5.     private void Awake()
    6.     {
    7.         text = GetComponent<Text>();
    8.     }
    9.  
    10.     private void Start()
    11.     {
    12.         originalPosition = transform.localPosition;
    13.         Set();
    14.     }
    15.  
    16.     private void Set()
    17.     {
    18.         transform.localPosition = originalPosition + Vector2.right * (text.preferredWidth % 2 == 0 ? 0f : 0.05f);
    19.         //transform.localPosition = originalPosition + Vector2.up * (text.preferredHeight % 2 == 0 ? 0f : 0.5f);
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         if (lastText != text.text)
    25.         {
    26.             lastText = text.text;
    27.             Set();
    28.         }
    29.     }
    I'd like to have each character also lock to a non-float position in a similar fashion, could it be done in the same script or would it be in a separate script on the textmesh object? Sorry for my inexperience with this!