UI 文本多行对齐不均匀

UI Text multiline uneven alignment

文本是一个未定义字数的字符串。对齐方式应该是这样的:

        The text is starting from here
    then when the line is over it
goes to the next line etc.

是否可以使用 UI 组件在 Unity 中以这种方式对齐文本?

不,没有办法内置。

请注意,Unity 提供 "text length" 功能 - 例如 iOS 提供 .

反正我实在想不出要找出"where it wraps"一行。也许,您可以逐个遍历每个字符,每次增加一个字符的字符串,当宽度停止增加时,您知道,它已经换行了。 (通过 .renderer.bounds.size.x 获取宽度)

我可能会鼓励您只说三个 UI.Text 并分隔字符串。简单地说,将字符串分成 50 个字符的块(在 space 处停止)或七个单词。 (它们的长度不会完全相同,但没问题。)

注意

如果您是新的 Unity 程序员或爱好者,通常解决此类问题的方法是

"USE AN ASSET!"

某个地方的某个人可能已经编写了您需要的程序。所以开始谷歌搜索类似 "free asset, wrap text around a shape" 或类似的东西。通常,给制作此类包裹的人发电子邮件是值得的,他们通常知道一些东西可以满足你的需要,如果他们的东西没有。

例子..http://forum.unity3d.com/threads/text-box.124906/

我很容易通过谷歌搜索 unity3d text utility wrap around a shape


下面是一些代码,用于拆分 LONG 行的合理文本,其中的行数限制为 50 个字符。注意文字必须是"reasonable",不能有长得离谱的词等

string wholeSentence = "Your whole sentence here ... goes on and on.";

List<string> words = new List<string>(
  wholeSentence
  .Split(new string[] { " " },
  StringSplitOptions.RemoveEmptyEntries)
  );

List<string> finalLines = new List<string>();

int count = 0;
string nextLine = "";
foreach (word w in words)
  {
  nextLine = nextLine + w + " ";
  count += w.Length;
  if (count>50)
   {
   finalLines.Add(nextLine);
   count=0;
   nextLine = "";
   }
  }

if (nextLine!="") finalLine.Add(nextLine);

那会给你所有的台词,在List "finalLines"!干杯