Ejemplo n.º 1
0
        /// <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;
                if (onFinished != null)
                {
                    onFinished(gameObject);
                }
                current = null;
            }
        }
Ejemplo n.º 2
0
        void Update()
        {
            if (!mActive)
            {
                return;
            }

            if (mReset)
            {
                mCurrentOffset = 0;
                mReset         = false;
                mLabel         = GetComponent <UILabel>();
                mFade.Clear();

                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;

                // 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 (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;
                if (onFinished != null)
                {
                    onFinished(gameObject);
                }
                current = null;
                mActive = false;
            }
        }