Get correct position of letters using cachedTextGenerator

空扰寡人 提交于 2019-12-02 11:12:43

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/35540406/get-correct-position-of-letters-using-cachedtextgenerator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!