zoukankan      html  css  js  c++  java
  • 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)?" ":"");
                    
                }
            }
        }
    }

    *文字过长会导致突然自动换行,用空格填充即可。

  • 相关阅读:
    hdu 4638 Group 线段树
    hdu 4635 Strongly connected 强连通分量
    hdu 4604 Deque
    hdu 1000 A + B Problem
    数组相关
    文本文件相关
    硬件电路中VCC,VDD,VEE,VSS有什么区别
    VIPM链接LabVIEW问题
    Touch实现轻扫
    touchesMoved 实现拖拽
  • 原文地址:https://www.cnblogs.com/CodeSnippet/p/7837640.html
Copyright © 2011-2022 走看看