在Unity使文字变色

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class ShipIntro : MonoBehaviour {
    public Image Arrow;
    public Image IntroBack;
    public Text txt;
    protected bool isstart=false;

    [Header("文本组")]
    public string mColorFrontText = "<color=#00FF00>";
    public string mColorBackText = "</color>";

    // Use this for initialization
    void Start () {
        StartCoroutine (PlayImage(Arrow,1/30f));
        StartCoroutine (PlayImage(IntroBack,1/30f));
        StartCoroutine (PlayWords(txt,3.0f,false));
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    IEnumerator PlayImage(Image img,float time)
    {
        while (img.fillAmount < 1) {
            img.fillAmount += Time.deltaTime;
            yield return new WaitForSeconds (time);
            if (img.fillAmount >= 1) {
                isstart = true;
                Debug.Log ("111");
            }
        }
    }


    IEnumerator PlayWords(Text txt,float time,bool isstart)
    {
        yield return new WaitForSeconds (2.0f);
        string sout = string.Empty;
        string s = "要变色的文字。";
        for (int i = 0; i <= s.Length; i++) {
            sout = s.Substring (0,i);
            txt.text = sout;
            yield return new WaitForSeconds (time/s.Length);
        }
        if (!isstart) {
            Debug.Log ("22");
            yield return new WaitForSeconds (3.0f);
            for(int i=0;i<s.Length;i++)
            {
                sout=mColorFrontText+s.Insert(i,mColorBackText);
                txt.text=sout;
                yield return new WaitForSeconds(time/s.Length);
            }
        }
    }
}

posted @ 2019-02-25 09:33  大颖嘤  阅读(1576)  评论(0编辑  收藏  举报