Unity UGUI打字机效果
using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; [RequireComponent(typeof(Text))] public class TextTyper : MonoBehaviour { public enum SplitMethod { BySpace, ByChar } public SplitMethod spliteMethod; public string content; public float deltaTime; public bool AutoPlay; bool isPlay = false; Text text; float tmpTime = 0; List<string> contentGroup; private void Start() { text = GetComponent<Text>(); switch (spliteMethod) { case SplitMethod.ByChar: contentGroup = new List<string>(); for (int i = 0; i < content.Length; i++) { contentGroup.Add(content.Substring(i, 1)); } break; case SplitMethod.BySpace: contentGroup = content.Split(' ').ToList(); break; } if (AutoPlay) Play(); } public void Play() { isPlay = true; index = 0; tmpTime = 0; } private int index; void Update() { if (isPlay) { if (index >= contentGroup.Count) { isPlay = false; } if ((tmpTime += Time.deltaTime) >= deltaTime) { tmpTime = 0; text.text += contentGroup[index++] + ((spliteMethod == SplitMethod.BySpace)?" ":""); } } } }
*文字过长会导致突然自动换行,用空格填充即可。
posted on 2017-11-15 10:24 吾少也贱,故能多鄙事。 阅读(2096) 评论(0) 编辑 收藏 举报