Typewriter text that fits label beforehand

Typewriter text that fits label beforehand
«  on: January 23, 2015, 12:12:59 AM »
So I'm using NGUI's example typewriter script to make dialogue "type itself out" when characters talk in the game via dialogue boxes.
所以我使用打字机同时的例子脚本使对话“类型本身”在游戏中人物说话时通过对话框。

The dialogue's label's overflow property is set to "Shrink content," so that the dialogue will automatically shrink if it's too big. I am trying to pre-shrink the dialogue before it's rendered because a dynamic shrink is jarring. I also try to insert line breaks at appropriate locations so that a word is not shoved to the next line as it's typed out -- it automatically goes to the next line because a newline is placed right before it.
对话的标签溢出属性设置为“减少内容,”这样的对话将会自动收缩,如果太大了。 我试图pre-shrink对话呈现之前因为动态收缩开裂。 我也尝试在适当的位置插入换行符,这样一个词不是推倒下一行的输入,它会自动到下一行,因为一个换行符之前。

To achieve this, I do the following:
为了达到这个目标,我做以下几点:

1. Give the label the message first, update it, and then record the scale value returned to influence the font size. I had to expose one variable as public first, which I didn't like to do. I don't know much about the internal workings of NGUI so this is something I thought of as an experiment.
1。 首先给标签信息,更新它,然后记录返回的刻度值影响字体大小。 我不得不暴露一个变量作为公共第一,我不喜欢去做。 我不了解内部运作的同时,这是我认为是一个实验。
Code:  [Select]
  // scale text first
  // NOTE: this code assumes that the text in this label scales!
  mCharDialogueMessageLabel.fontSize = 45;
  mCharDialogueMessageLabel.text = message;
  mCharDialogueMessageLabel.Update();
  mCharDialogueMessageLabel.fontSize =     Mathf.FloorToInt(mCharDialogueMessageLabel.fontSize*mCharDialogueMessageLabel.scale);
  mCharDialogueMessageLabel.text = string.Empty;
  mCharDialogueMessageLabel.Update();

2. After the font is scaled by NGUI (or maybe not??), insert line breaks at the correct locations
2。 字体后扩大的同时(或也许不是? ?),插入换行符在正确的位置
Code:  [Select]
    // account for line breaks at proper positions (i.e. so text doesn't wrap to next line)
    // AFTER scaling text properly (since text needs to fit label first, then adjusted for word wrap)
    mCharDialogueMessageLabel.UpdateNGUIText();
    char[] delimiterChars = { ' ' };
    string [] parts = message.Split(delimiterChars);
    // add words to the modified string
    StringBuilder currLine = new StringBuilder();
    StringBuilder overallText = new StringBuilder();
    for (int i = 0; i < parts.Length; i++) 
    {
      string currWord = parts[i];
      if (currLine.Length > 0)
        currLine.Append(' ');
      currLine.Append(currWord);

      Vector2 printedSize = NGUIText.CalculatePrintedSize(currLine.ToString());
      // this is the part where we find out if we went to next line...if so, then
      // we need a line break
      if ((int)printedSize.y > mCharDialogueMessageLabel.fontSize)
      {
        currLine.Remove(currLine.Length-currWord.Length, currWord.Length);
        // remove white space at the end, otherwise it will mess up the new line
        overallText.Append(currLine.ToString().TrimEnd(delimiterChars));
        overallText.Append('\n');
        // current line starts over with current word (since we need a new line)
        currLine.Remove(0, currLine.Length);
        currLine.Append(currWord);
      }
    }
    // currline is within printed size, just add it
    overallText.Append(currLine);
    // overallText is then given to the typewriter

Anyway, this code seems to work for the most part even though I'm kind of shooting in the dark. But there is an example where it doesn't work, as seen in this video:

无论如何,这段代码似乎工作在大多数情况下,即使我在黑暗中射击。 但就是这样一个例子,它不工作,可以看到在这个视频:
https://www.dropbox.com/s/g1wh1exumngogvj/DialogueTypingError.mov?dl=0

Notice how the dialogue shrinks at the end? I want to avoid that -- it should be scaled appropriately beforehand. I believe that if I print out the fontscale after the typewriter is finished, it's different compared to the scale computed above. Anyway, just wondering if there is a reasonable solution to this or if this has been done.
注意对话结束时收缩?我想避免这种情况,事先应予以适当扩展。我相信如果我打印fontscale打字机完成后,上面的计算规模相比,情况是不同的。无论如何,只是想知道如果有一个合理的解决方案,或者这已经完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值