UGUI 如何实现文本打字效果

发表于2017-10-16
评论0 2.3k浏览

熟悉NGUI的人肯定知道文本打字效果的实现方法,可能有些人觉得NGUI弄的有些繁琐, 可以尝试将NGUI的TypewriterEffect类转成 UGUI特定的的方式实现文本打字效果。


这里只是简化的内容,但足够大家大家去看。


  1. using System;  
  2. using UnityEngine;  
  3. using UnityEngine.UI;  
  4. using UnityEngine.Events;  
  5.   
  6. /// <summary>  
  7. /// 此脚本是能够将文本字符串随着时间打字或褪色显示。  
  8. /// </summary>  
  9.   
  10. [RequireComponent(typeof(Text))]  
  11. [AddComponentMenu("Typewriter Effect")]  
  12. public class TypewriterEffect : MonoBehaviour  
  13. {  
  14.     public UnityEvent myEvent;  
  15.     public int charsPerSecond = 0;  
  16.     // public AudioClip mAudioClip;             // 打字的声音,不是没打一个字播放一下,开始的时候播放结束就停止播放  
  17.     private bool isActive = false;  
  18.   
  19.     private float timer;  
  20.     private string words;  
  21.     private Text mText;  
  22.   
  23.     void Start()  
  24.     {  
  25.         if (myEvent == null)  
  26.             myEvent = new UnityEvent();  
  27.   
  28.         words = GetComponent<Text>().text;  
  29.         GetComponent<Text>().text = string.Empty;  
  30.         timer = 0;  
  31.         isActive = true;  
  32.         charsPerSecond = Mathf.Max(1, charsPerSecond);  
  33.         mText = GetComponent<Text>();  
  34.     }  
  35.   
  36.     void ReloadText()  
  37.     {  
  38.         words = GetComponent<Text>().text;  
  39.         mText = GetComponent<Text>();  
  40.     }  
  41.   
  42.     public void OnStart()  
  43.     {  
  44.         ReloadText();  
  45.         isActive = true;  
  46.     }  
  47.   
  48.     void OnStartWriter()  
  49.     {  
  50.         if (isActive)  
  51.         {  
  52.             try  
  53.             {  
  54.                 mText.text = words.Substring(0, (int) (charsPerSecond * timer));  
  55.                 timer  = Time.deltaTime;  
  56.             }  
  57.             catch (Exception)  
  58.             {  
  59.                 OnFinish();  
  60.             }  
  61.         }  
  62.     }  
  63.   
  64.     void OnFinish()  
  65.     {  
  66.         isActive = false;  
  67.         timer = 0;  
  68.         GetComponent<Text>().text = words;  
  69.         try  
  70.         {  
  71.              myEvent.Invoke();  
  72.         }  
  73.         catch (Exception)  
  74.         {  
  75.             Debug.Log("问题");  
  76.         }  
  77.     }  
  78.   
  79.     void Update()  
  80.     {  
  81.         OnStartWriter();  
  82.     }  
  83. }  

还没有完


想使用前几天 介绍的插件 TextFX 实现这个功能:


还有就是使用 插件 DoTween 实现这个动能:

Text (Unity UI 4.6)

DOText(string to, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
Tweens the target's text to the given value. 
richTextEnabled If TRUE (default), rich text will be interpreted correctly while animated, otherwise all tags will be considered as normal text. 
scramble The type of scramble mode to use, if any. 
If different than ScrambleMode.None the string will appear from a random animation of characters, otherwise it will compose itself regularly. 
None (default): no scrambling will be applied. 
All/Uppercase/Lowercase/Numerals: type of characters to be used while scrambling. 
Custom: will use the custom characters in scrambleChars. 
scrambleChars A string containing the characters to use for custom scrambling. Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引