没接触NGUI之前,因为有个老师跟我们讲,NGUI这个东西已经被淘汰啦 bla bla bla
然后有个师兄在外面找U3D的工作,回来之后,面试 问到 NGUI ,没学过这个东西啊.. bla bla bla
之前用UGUI方法做的打字机思路是这样, 在开始时,把字体存在一个string变量里面,然后在update 里用equal去判断是否完成,再 将其substr 一下。字符截取一下。
下面的源码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextTween : MonoBehaviour {
Text m_text;
string TextString;
float Timer=0f;
bool isFinish;
public float Speed=2f;
// Use this for initialization
void Start ()
{ m_text = GetComponent<Text> ();
TextString = m_text.text;
m_text.text = "";
}
// Update is called once per frame
void Update () {
if (m_text.text.Equals (TextString)) {
isFinish = true;
//字体播放结束 finish 执行 Finsh
}
else
{
m_text.text = TextString.Substring (0, (int)(Timer * Speed));
Timer += Time.deltaTime;
}
}
}
而 NGUI 呢。 主要在
在 typewriterEffect中封装好了
chars per Second就是每秒多少字符。
Fade in Time 就是 Fade后显示的时间。
Delay On period 就是 在一句后的 延迟。
Delay On New Line 呢就是 每一行后的延迟。
Scoll view 就是滚动栏啦。其实意思就是 跟随滚动的意思。
源码如下
//-------------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2018 Tasharen Entertainment Inc
//-------------------------------------------------
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>
Chat快问 new
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;
mNextChar = 0f;
mCurrentOffset = 0;
Update();
}
/// <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 OnDisable () { Finish(); }
void Update ()
{
if (!mActive) return;
if (mReset)
{
mCurrentOffset = 0;
mReset = false;
mLabel = GetComponent<UILabel>();
mFullText = mLabel.processedText;
mFade.Clear();
if (keepFullDimensions && scrollView != null) scrollView.UpdatePosition();
}
if (string.IsNullOrEmpty(mFullText)) return;
var len = mFullText.Length;
while (mCurrentOffset < len && mNextChar <= RealTime.time)
{
int lastOffset = mCurrentOffset;
charsPerSecond = Mathf.Max(1, charsPerSecond);
// Automatically skip all symbols
if (mLabel.supportEncoding)
while (NGUIText.ParseSymbol(mFullText, ref mCurrentOffset)) { }
++mCurrentOffset;
// Reached the end? We're done.
if (mCurrentOffset > len) break;
// Periods and end-of-line characters should pause for a longer time.
float delay = 1f / charsPerSecond;
char c = (lastOffset < len) ? mFullText[lastOffset] : '\n';
if (c == '\n')
{
delay += delayOnNewLine;
}
else if (lastOffset + 1 == len || mFullText[lastOffset + 1] <= ' ')
{
if (c == '.')
{
if (lastOffset + 2 < len && 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 (mCurrentOffset >= len && mFade.size == 0)
{
mLabel.text = mFullText;
current = this;
EventDelegate.Execute(onFinished);
current = null;
mActive = false;
}
else 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();
}
}
}
}
scrollView.UpdatePosition();
StringBuilder sb = new StringBuilder();
这个 是为了减少内存的使用,优化方面的。
就是 更新位置啦,
做打字机效果