0

Consider a Text with many paragraphs of text. It's easy to find where a certain character is, using UICharInfo.

Example, find all newlines...

TextGenerator tg = text.cachedTextGenerator;
int kText = tg.characterCount;
for (int i=0; i < kText; ++i)
  {
  if ( text.text[i] == '\n' )
    Debug.Log("found a newline at " + tg.verts[i * 4].position.y);
  }

This gives the correct ratios but they are all out by some scaling factor.

It seems to depend on many things (screen shape, pixels, settings and more).

Naturally this is in a Canvas using scale-with-screen-size.

How do you get the actual offset of a character - to use in a scroll view to scroll to that point?

3
  • will be adding enormous bounty .. maybe more than 12 points for this as soon as the bounty period is open. get in now to profit
    – Fattie
    Feb 22, 2016 at 17:41
  • "maybe more than 12 points" - lol :)
    – user585968
    Mar 4, 2016 at 7:31
  • HEH ... come and get it, programmers!
    – Fattie
    Mar 4, 2016 at 12:50

1 Answer 1

1
+50

It uses a combination of text.pixelsPerUnit and what they call a rounding offset. The code they use is open source and can be found here. The relevant lines you are looking for are in the function

protected override void OnPopulateMesh(VertexHelper toFill)

All the functions they use to calculate them are public and should be easy enough to duplicate on your side

2
  • an incredible tip Colton. I will look in to it.
    – Fattie
    Feb 22, 2016 at 16:47
  • Colton, you deservedly win the bounty -- thanks! Another SEC win :) I think I'll start another bounty question to try to get "someone to write code for this" heh
    – Fattie
    Mar 9, 2016 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.