31
NGUI 3 Support / [SOLVED] Re-use the TypeWriterEffect instead of destroying it?
« on: September 15, 2013, 12:39:06 PM »
Hello,
I just hacked (modified) TypeWriterEffect to not destroy itself when it finishes. Instead, there's a boolean 'Begin' that you set to true each time you want the effect to occur.
First, I tried making a 'Begin' method, and forget about Update:
The problem with that, is that it gets stuck between the condition of the while and the if (infinite loop) because the method is not being called in Update, I guess...
So I thought I should use update, so I came up with this idea of using a boolean Begin, set it to true each time I want the effect to happen:
Is this good? is there a better way you might suggest? I would love to not use Update, and use a separate method like in my first try - Thanks.
I just hacked (modified) TypeWriterEffect to not destroy itself when it finishes. Instead, there's a boolean 'Begin' that you set to true each time you want the effect to occur.
First, I tried making a 'Begin' method, and forget about Update:
- public void Begin()
- {
- if (mLabel == null)
- {
- mLabel = GetComponent<UILabel>();
- mLabel.supportEncoding = false;
- mLabel.symbolStyle = UIFont.SymbolStyle.None;
- mText = mLabel.font.WrapText(mLabel.text, mLabel.lineWidth / mLabel.cachedTransform.localScale.x,
- mLabel.maxLineCount, false, UIFont.SymbolStyle.None);
- }
- while (mOffset < mText.Length)
- {
- if (mNextChar <= Time.time)
- {
- charsPerSecond = Mathf.Max(1, charsPerSecond);
- // Periods and end-of-line characters should pause for a longer time.
- float delay = 1f / charsPerSecond;
- char c = mText[mOffset];
- if (c == '.' || c == '\n' || c == '!' || c == '?') delay *= 4f;
- mNextChar = Time.time + delay;
- mLabel.text = mText.Substring(0, ++mOffset);
- }
- }
- }
The problem with that, is that it gets stuck between the condition of the while and the if (infinite loop) because the method is not being called in Update, I guess...
So I thought I should use update, so I came up with this idea of using a boolean Begin, set it to true each time I want the effect to happen:
- private bool begin;
- public bool Begin {get {return begin;} set{begin = value; Reset();}}
- private void Reset()
- {
- mOffset = 0;
- mNextChar = 0;
- if (mLabel != null)
- mText = mLabel.font.WrapText(mLabel.text, mLabel.lineWidth / mLabel.cachedTransform.localScale.x,
- mLabel.maxLineCount, false, UIFont.SymbolStyle.None);
- }
- void Update()
- {
- if (begin)
- {
- if (mLabel == null)
- {
- mLabel = GetComponent<UILabel>();
- mLabel.supportEncoding = false;
- mLabel.symbolStyle = UIFont.SymbolStyle.None;
- mText = mLabel.font.WrapText(mLabel.text, mLabel.lineWidth/mLabel.cachedTransform.localScale.x,
- mLabel.maxLineCount, false, UIFont.SymbolStyle.None);
- }
- if (mOffset < mText.Length)
- {
- if (mNextChar <= Time.time)
- {
- charsPerSecond = Mathf.Max(1, charsPerSecond);
- // Periods and end-of-line characters should pause for a longer time.
- float delay = 1f/charsPerSecond;
- char c = mText[mOffset];
- if (c == '.' || c == '\n' || c == '!' || c == '?') delay *= 4f;
- mNextChar = Time.time + delay;
- mLabel.text = mText.Substring(0, ++mOffset);
- }
- }
- }// else Destroy(this);
- else begin = false;
- }
Is this good? is there a better way you might suggest? I would love to not use Update, and use a separate method like in my first try - Thanks.