NGUI中打字效果TypewriterEffect的一个BUG
来源:互联网 发布:js中的换行是什么 编辑:程序博客网 时间:2024/06/16 09:34
项目中使用NGUI时用到了打字效果,NGUI自带有这样的脚本,效果还是不错的。按要求设置,一切正常。
但是脚本的使用并不是用完就OK,可能会有多次使用,每次都要求这种效果,也就是同一个脚本,多次使用。一般来说,只要下次使用前reset恢复到原始参数就行了。
但是问题来了,运行第一次是正常的,后面老是莫名其妙的没打字效果,api方面能用的函数就ResetToBeginning和Finish两个函数,看源码Finish不是,那就是ResetToBeginning的问题了,可能有写东西没重置成功。
断点,代码正常执行,断点查看打字效果也是有的。
再次查看,发现问题,每帧运行都更改了字符长度,难道是打字速度问题。调整参数,无效。
再次断点,两个判断条件 (mCurrentOffset < mFullText.Length && mNextChar <= RealTime.time),
第一个成立很正常,问题就只能出现在第二个条件了,mNextChar <= RealTime.time,第一遍正常,后面有问题,断点查看mNextChar 的参数很容易查出mNextChar 没有在reset是重置。
问题找出来了,解决办法就是在update的重置区里面加上对应的重置语句就行了。mNextChar = 0;
完整代码
//----------------------------------------------// NGUI: Next-Gen UI kit// Copyright © 2011-2014 Tasharen Entertainment//----------------------------------------------using UnityEngine;using System.Text;using System.Collections.Generic;/// <summary>/// This script is able to fill in the label's text gradually, giving the effect of someone typing or fading in the content over time./// </summary>[RequireComponent(typeof(UILabel))][AddComponentMenu("NGUI/Interaction/Typewriter Effect")]public class TypewriterEffect : MonoBehaviour{ static public TypewriterEffect current; struct FadeEntry { public int index; public string text; public float alpha; } /// <summary> /// How many characters will be printed per second. /// </summary> public int charsPerSecond = 20; /// <summary> /// How long it takes for each character to fade in. /// </summary> public float fadeInTime = 0f; /// <summary> /// How long to pause when a period is encountered (in seconds). /// </summary> public float delayOnPeriod = 0f; /// <summary> /// How long to pause when a new line character is encountered (in seconds). /// </summary> public float delayOnNewLine = 0f; /// <summary> /// If a scroll view is specified, its UpdatePosition() function will be called every time the text is updated. /// </summary> public UIScrollView scrollView; /// <summary> /// If set to 'true', the label's dimensions will be that of a fully faded-in content. /// </summary> public bool keepFullDimensions = false; /// <summary> /// Event delegate triggered when the typewriter effect finishes. /// </summary> public List<EventDelegate> onFinished = new List<EventDelegate>(); UILabel mLabel; string mFullText = ""; int mCurrentOffset = 0; float mNextChar = 0f; bool mReset = true; bool mActive = false; BetterList<FadeEntry> mFade = new BetterList<FadeEntry>(); /// <summary> /// Whether the typewriter effect is currently active or not. /// </summary> public bool isActive { get { return mActive; } } /// <summary> /// Reset the typewriter effect to the beginning of the label. /// </summary> public void ResetToBeginning () { Finish(); mReset = true; mActive = true; } /// <summary> /// Finish the typewriter operation and show all the text right away. /// </summary> public void Finish () { if (mActive) { mActive = false; if (!mReset) { mCurrentOffset = mFullText.Length; mFade.Clear(); mLabel.text = mFullText; } if (keepFullDimensions && scrollView != null) scrollView.UpdatePosition(); current = this; EventDelegate.Execute(onFinished); current = null; } } void OnEnable () { mReset = true; mActive = true; } void Update () { if (!mActive) return; if (mReset) { mCurrentOffset = 0; mReset = false; mLabel = GetComponent<UILabel>(); mFullText = mLabel.processedText; mFade.Clear(); mNextChar = 0; if (keepFullDimensions && scrollView != null) scrollView.UpdatePosition(); } while (mCurrentOffset < mFullText.Length && mNextChar <= RealTime.time) { int lastOffset = mCurrentOffset; charsPerSecond = Mathf.Max(1, charsPerSecond); // Automatically skip all symbols while (NGUIText.ParseSymbol(mFullText, ref mCurrentOffset)) { } ++mCurrentOffset; // Periods and end-of-line characters should pause for a longer time. float delay = 1f / charsPerSecond; char c = (lastOffset < mFullText.Length) ? mFullText[lastOffset] : '\n'; if (c == '\n') { delay += delayOnNewLine; } else if (lastOffset + 1 == mFullText.Length || mFullText[lastOffset + 1] <= ' ') { if (c == '.') { if (lastOffset + 2 < mFullText.Length && mFullText[lastOffset + 1] == '.' && mFullText[lastOffset + 2] == '.') { delay += delayOnPeriod * 3f; lastOffset += 2; } else delay += delayOnPeriod; } else if (c == '!' || c == '?') { delay += delayOnPeriod; } } if (mNextChar == 0f) { mNextChar = RealTime.time + delay; } else mNextChar += delay; if (fadeInTime != 0f) { // There is smooth fading involved FadeEntry fe = new FadeEntry(); fe.index = lastOffset; fe.alpha = 0f; fe.text = mFullText.Substring(lastOffset, mCurrentOffset - lastOffset); mFade.Add(fe); } else { // No smooth fading necessary mLabel.text = keepFullDimensions ? mFullText.Substring(0, mCurrentOffset) + "[00]" + mFullText.Substring(mCurrentOffset) : mFullText.Substring(0, mCurrentOffset); // If a scroll view was specified, update its position if (!keepFullDimensions && scrollView != null) scrollView.UpdatePosition(); } } // Alpha-based fading if (mFade.size != 0) { for (int i = 0; i < mFade.size; ) { FadeEntry fe = mFade[i]; fe.alpha += RealTime.deltaTime / fadeInTime; if (fe.alpha < 1f) { mFade[i] = fe; ++i; } else mFade.RemoveAt(i); } if (mFade.size == 0) { if (keepFullDimensions) mLabel.text = mFullText.Substring(0, mCurrentOffset) + "[00]" + mFullText.Substring(mCurrentOffset); else mLabel.text = mFullText.Substring(0, mCurrentOffset); } else { StringBuilder sb = new StringBuilder(); for (int i = 0; i < mFade.size; ++i) { FadeEntry fe = mFade[i]; if (i == 0) { sb.Append(mFullText.Substring(0, fe.index)); } sb.Append('['); sb.Append(NGUIText.EncodeAlpha(fe.alpha)); sb.Append(']'); sb.Append(fe.text); } if (keepFullDimensions) { sb.Append("[00]"); sb.Append(mFullText.Substring(mCurrentOffset)); } mLabel.text = sb.ToString(); } } else if (mCurrentOffset == mFullText.Length) { current = this; EventDelegate.Execute(onFinished); current = null; mActive = false; } }}
0 0
- NGUI中打字效果TypewriterEffect的一个BUG
- NGUI的打字效果typewirter Effect
- NGUI的一个bug解决
- unity中实现键盘打字的效果
- 打字效果的文字特效
- 一个关于NGUI的点击和按键响应的Bug
- 打字效果的文字广播
- 简单的页面打字效果
- 打字效果
- 打字效果的带链接的新闻标题
- The Text Typewriter Effect Use Colors(NGUI打字效果加上颜色)
- JS实现简单的打字效果
- 非常不错的一款打字代码效果
- Unity的NGUI的 sprite的bug
- cocos2dx粒子效果的一个有趣的bug
- 最近一个项目中关于NGUI部分的总结
- NGUI中TweenScale回调函数设为null时导致的BUG
- NGUI在Unity中因更改窗口区域而出现Bug的处理
- 懒加载时出错
- NLPIR(ICTCLAS2016)对词频统计功能
- 用sql代码集合方式挑选质数、素数
- 从文法的定义讲起
- mknod
- NGUI中打字效果TypewriterEffect的一个BUG
- 比较好用的【图床】浏览器插件&站点
- Linux获取进程pid
- 77. Combinations
- private权限
- noip 2010 关押罪犯
- 第3周-项目4-谁是小偷
- 设计模式之动态代理模式(一)
- 解析URL的php方法