unity 字幕效果(整句)

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextAEvent : MonoBehaviour
{

    public Transform panelA;
  
    [Header("UI 组件")]
    public Text _TextLabel;//文本框

    [Header("文本文件")]
    public TextAsset _TextFile;
    public int _Index;//序号
    //字符显示速度
    public float _TextSpeed = 0.1f;
    bool po = false;
    //文本列表
    List<string> _TextList = new List<string>();
   

    void Awake()
    {
        GetTextFormFile(_TextFile);
        _Index = 0;//序列归零
    }
    private void OnEnable()
    {
       _Index = 0;//序列归零
                   //开启协程
        StartCoroutine(SetTextUI());
    }

    void Update()
    {
     
        if (_Index == _TextList.Count)
        {
            transform.gameObject.SetActive(false);
            _Index = 0;//序列归零
            panelA.gameObject.SetActive(true);
            return;
        }
    }
    //文件读取方法
    
    private void GetTextFormFile(TextAsset _Flie)
    {
        _TextList.Clear();
        _Index = 0;
        //逐行读取文本文件
        var _LineDate = _Flie.text.Split('\n');
        foreach (var _Line in _LineDate)
        {
            _TextList.Add(_Line);
        }

    }

    //文本输出方法
    public IEnumerator SetTextUI()
    {
        for (int i = 0; i <_TextList.Count ; i++)
        {
            _TextLabel.text = "";
            _TextLabel.text = _TextList[_Index];
            yield return new WaitForSeconds(_TextSpeed);
            _Index++;
        }
    }

猜你喜欢

转载自blog.csdn.net/LiPing122335/article/details/124170859