使用cachedTextGenerator获取字母的正确位置 [英] Get correct position of letters using cachedTextGenerator

查看:789
本文介绍了使用cachedTextGenerator获取字母的正确位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个Text包含许多文本段落.使用 UICharInfo ,可以很容易找到某个字符在哪里.

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).

自然地,这是在Canvas中使用屏幕缩放比例.

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

如何获取字符的实际偏移量-在滚动视图中滚动到该点?

推荐答案

它结合使用text.pixelsPerUnit和它们称为舍入偏移量的组合.他们使用的代码是开源的,可以在

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

这篇关于使用cachedTextGenerator获取字母的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆