コード例 #1
0
    //改變顏色
    private IEnumerator ChangeColorEnumerator(ColorScript info)
    {
        bool  playing     = true;
        float timer       = 0;                     //計時器
        float endTime     = info.GetEndTime();     //動畫結束時間
        float denominator = info.GetDenominator(); //動畫進度分母
        Image img         = this.GetComponent <Image>();

        if (!img.enabled)
        {
            img.enabled = true;
        }

        Color32[] alteration = new Color32[2] {
            info.insOutsColors.Length == 2 ? info.insOutsColors[0] : (Color32)this.GetComponent <Image>().color, info.insOutsColors[info.insOutsColors.Length - 1]
        };                                                                                                                                                                                               //重新定義起始與目標顏色
        info.insOutsColors = alteration;

        while (playing) //變化過程
        {
            timer = timer + Time.fixedDeltaTime > endTime ? endTime : timer + Time.fixedDeltaTime;

            float r = alteration[0].r + ((alteration[1].r - alteration[0].r) * (info.GetNumerator(timer) / denominator));
            float g = alteration[0].g + ((alteration[1].g - alteration[0].g) * (info.GetNumerator(timer) / denominator));
            float b = alteration[0].b + ((alteration[1].b - alteration[0].b) * (info.GetNumerator(timer) / denominator));
            float a = alteration[0].a + ((alteration[1].a - alteration[0].a) * (info.GetNumerator(timer) / denominator));
            img.color = new Color32((byte)Mathf.Clamp(r, 0, 255), (byte)Mathf.Clamp(g, 0, 255), (byte)Mathf.Clamp(b, 0, 255), (byte)Mathf.Clamp(a, 0, 255));

            if (timer == endTime)
            {
                playing = false;
            }
            yield return(new WaitForFixedUpdate());
        }

        info.isPlaying = false;

        yield return(null);
    }