首页 > 编程语言 > 详细

unity实现打字效果(例如游戏中的NPC对话面板)

时间:2019-03-22 14:59:11      阅读:602      评论:0      收藏:0      [点我收藏+]
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class SelfWritingText : MonoBehaviour
{
    [SerializeField] private Text textToUse;
    [SerializeField] private bool useThisText = false;
    [SerializeField] private bool useThisTextText = false;
    [SerializeField] private float letterPause = 0.1f;
    [TextAreaAttribute(4, 15)]
    [SerializeField]
    public string message;

    private void Start()
    {
        //message = textToShow[0];
        if (useThisText)
        {
            textToUse = GetComponent<Text>();
        }
        if (useThisTextText)
        {
            message = textToUse.text;
        }
        textToUse.text = "";
        StartCoroutine(TypeText(textToUse, message, letterPause));
    }

    private IEnumerator TypeText(Text text, string textText, float timePause)
    {
        for (int i = 0; i < textText.Length; i++)
        {
            text.text += textText[i];
            yield return 0;
            yield return new WaitForSeconds(timePause);

        }
    }
    public void WriteText(Text newText = null, string newTextToShow = null, float newLetterPause = -1.0f)
    {
        if (newText != null && newTextToShow != null && newLetterPause > 0.0f)
        {
            StartCoroutine(TypeText(newText, newTextToShow, newLetterPause));
            return;
        }
        if (newText != null && newTextToShow != null)
        {
            StartCoroutine(TypeText(newText, newTextToShow, letterPause));
            return;
        }
        if (newText != null && newLetterPause > 0.0f)
        {
            StartCoroutine(TypeText(newText, message, newLetterPause));
            return;
        }
        if (newTextToShow != null && newLetterPause > 0.0f)
        {
            StartCoroutine(TypeText(textToUse, newTextToShow, newLetterPause));
            return;
        }
        if (newTextToShow != null)
        {
            StartCoroutine(TypeText(textToUse, newTextToShow, letterPause));
            return;
        }
        if (newLetterPause > 0.0f)
        {
            StartCoroutine(TypeText(textToUse, message, letterPause));
            return;
        }
    }
}

  

unity实现打字效果(例如游戏中的NPC对话面板)

原文:https://www.cnblogs.com/fangshiyuanzhucheng/p/10577780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!