天天看點

C#協程實作 UGUI :Ready和GO! 兩個Text 先後淡出

效果圖:

C#協程實作 UGUI :Ready和GO! 兩個Text 先後淡出

上代碼:

public class TextFadeOut : MonoBehaviour {

    public float speed = 0.5f;

    Text text;

    Color color;

    void Start () {

        text = GetComponent<Text>();

        color = text.color;

}

void Update () {

        if (gameObject.activeSelf)

        {

            color.a -= Time.deltaTime * speed;

            text.color = color;

        }

}

}

注意:兩個Text——"Ready"  "GO!" 都要挂上TextFadeOut腳本。并且兩個Text,先禁用隐藏掉。

public class PrepareLevel : MonoBehaviour {

    public GameObject GetReady;

    public GameObject Go;

void Start () {

        StartCoroutine(PrepareRoutine());

}

    IEnumerator PrepareRoutine() {

        yield return new WaitForSeconds(1f);

        GetReady.SetActive(true);

        yield return new WaitForSeconds(2f);

        GetReady.SetActive(false);

        Go.SetActive(true);

        yield return new WaitForSeconds(1f);

        Go.SetActive(false);

    }

}

注意:建立一個空物體,名字改為GameController,然後将PrepareLevel 腳本挂在它上面。