您的位置:首页 > 其它

滚动字幕的制作

2017-06-19 20:22 274 查看
在本游戏中,加载场景的时候,为了防止过长的等待时间导致玩家觉得无聊,所以在加载场景中添加了一段滚动字幕的介绍。用来介绍一些游戏的背景之类的知识。

滚动字幕本身的制作比较简单。只需要用到GUI.Label(new Rect())方法即可,这个方法有四个参数,分别用来表示新建出的Label的x,y位置以及Label的长度和宽度。

GUI.Label(new Rect(300, hight, 200, 400), "这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍,\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"字效果这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍 320\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍2009-06\n" +
"-这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍。这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍,这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍\n" +
"这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍这是第一段的游戏介绍");

如上代码,即可以在游戏画面的x=300和y=height的地方出现一些文字。由于我们需要一个可以上下滚动的字幕,所以需要该文字的y轴位置是可变的,这里用参数height来表示。

if (hight > 0)
{
hight -= Time.deltaTime * 10;
}

一种比较简单的滚动方法是,让height随着时间变化而变化,这样字幕的滚动效果就是按照时间的变化,不断向上滚动。



就可以达到类似于这样的效果,文字从最下端向上滚动。

这里注意需要为面板添加一个类似于遮罩的东西。比较快速的方法是在UI Panel中设置参数为SOFT CLIPPING,这样就可以将设定区域以外的子组件剪除掉。

除此以外,还可以用typing的方式来表示文字。这是一种NGUI自带的方法。

具体代码如下:

using UnityEngine;
using System.Text;
using System.Collections.Generic;

[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.
/// </su
9aeb
mmary>

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;

while (mCurrentOffset < mFullText.Length && 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 > mFullText.Length) break;

// 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 (mCurrentOffset >= mFullText.Length)
{
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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: