unity实现材质球渐隐渐现(无缝衔接)




using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StandbyController : MonoBehaviour
{

    public GameObject[] gameObjects;//渐隐的所有物体 4
    public Material[] mats;//所有物体的材质 4
    public Color startColor;
    public Color endColor;

    private int matIndex;

    private void Start()
    {
        for (int i = 1; i < gameObjects.Length; i++)
        {
            gameObjects[i].SetActive(false);
            mats[i].color = endColor;
        }

        matIndex = 0;
        InvokeRepeating("GraduallyMat", 10, 10);
    }

    private void GraduallyMat()
    {
        StartCoroutine("IEGraduallyMat");
    }

    private IEnumerator IEGraduallyMat()
    {
        if (mats[matIndex].color == startColor)
        {
            for (int i = 0; i < 100; i++)
            {
                yield return new WaitForSeconds(0.01f);
                mats[matIndex].color = Color.Lerp(mats[matIndex].color, endColor, Time.deltaTime * 2f);
            }
        }
        else if (mats[matIndex].color == endColor)
        {
            for (int i = 0; i < 100; i++)
            {
                yield return new WaitForSeconds(0.01f);
                mats[matIndex].color = Color.Lerp(mats[matIndex].color, startColor, Time.deltaTime * 2f);
            }
        }


        gameObjects[matIndex].SetActive(false);
        mats[matIndex].color = startColor;
        matIndex = matIndex + 1;
        if (matIndex > 3)
        {
            matIndex = 0;
        }
        mats[matIndex].color = endColor;
        gameObjects[matIndex].SetActive(true);


        if (mats[matIndex].color == startColor)
        {
            for (int i = 0; i < 100; i++)
            {
                yield return new WaitForSeconds(0.01f);
                mats[matIndex].color = Color.Lerp(mats[matIndex].color, endColor, Time.deltaTime * 2f);
            }
        }
        else if (mats[matIndex].color == endColor)
        {
            for (int i = 0; i < 100; i++)
            {
                yield return new WaitForSeconds(0.01f);
                mats[matIndex].color = Color.Lerp(mats[matIndex].color, startColor, Time.deltaTime * 2f);
            }
        }
        mats[matIndex].color = startColor;
    }
}
发布了23 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42047805/article/details/94464630
今日推荐