首页 > 代码库 > 【Unity】透明度渐变

【Unity】透明度渐变

效果图如下:

技术分享

代码;

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

public class TransEffect : MonoBehaviour
{

    public List<GoInfo> GoList;
    public float varifySpeed = 0.5f;
    public float aTime = 5f;//每个物体保持出现的时间
    public float dTime = 5f;

    private float minAlpha = 0.0f;
    private float maxAlpha = .9f;
    private float curAlpha = 1.0f;
    private float nextAlpha = 0.0f;
    private int i = 0;

    public void OnEnable()
    {
        LoadGo();
    }

    // Use this for initialization
    void Start()
    {
        //初始化全List隐形
        foreach (GoInfo go in GoList)
        {
                Color c = go.rend.material.color;
                c.a = 0;
                go.rend.material.color = c;
            
        }
    }

    // Update is called once per frame
    public void Update()
    {
        Trans();
    }


    void LoadGo()
    {
        GoList = new List<GoInfo>();
        GoList.Add(new GoInfo("Cylinder", 0, transform.Find("Cylinder").GetComponent<GameObject>(), transform.Find("Cylinder").GetComponent<MeshRenderer>()));
        GoList.Add(new GoInfo("Cube", 1, transform.Find("Cube").GetComponent<GameObject>(), transform.Find("Cube").GetComponent<MeshRenderer>()));
        GoList.Add(new GoInfo("Sphere", 2, transform.Find("Sphere").GetComponent<GameObject>(), transform.Find("Sphere").GetComponent<MeshRenderer>()));
        GoList.Add(new GoInfo("Capsule", 3, transform.Find("Capsule").GetComponent<GameObject>(), transform.Find("Capsule").GetComponent<MeshRenderer>()));
    }

private void Trans()
    {
        GoInfo go = GoList[i];
        GoInfo nextgo;
        Color c = go.rend.material.color;
        Color nextc = go.rend.material.color;

        if (i <= GoList.Count)
        {
            if (i == GoList.Count - 1)
            {
                nextgo = GoList[0];
            }
            else
            {
                nextgo = GoList[i + 1];
            }

            Debug.Log(nextAlpha);
            Debug.Log(curAlpha);

            if (Time.time < aTime)//当前物体保持显形
            {
                c.a = 1;
                go.rend.material.color = c;
            }
            else if (Time.time >= aTime)
            {
                curAlpha += Time.deltaTime * varifySpeed * (-1);//当前物体逐渐消失
                nextAlpha += Time.deltaTime * varifySpeed;//下一个物体逐渐现形

                if (curAlpha <= minAlpha)//当前物体渐变到不透明时
                {
                    c.a = 0;//设置当前obj保持透明
                    go.rend.material.color = c;
                    i++;
                    //设置数据为下一物体做准备
                    curAlpha = 1;
                    nextAlpha = 0;
                }

                else//当前物体逐渐透明,下一物体逐渐现形
                {
                    curAlpha = Mathf.Clamp(curAlpha, minAlpha, maxAlpha);
                    nextAlpha = Mathf.Clamp(nextAlpha, minAlpha, maxAlpha);
                    c.a = curAlpha;
                    nextc.a = nextAlpha;
                    go.rend.material.color = c;
                    nextgo.rend.material.color = nextc;

                }

                if (curAlpha >= maxAlpha)//下一物体完全显形
                {
                    Debug.Log(nextAlpha);
                    Debug.Log(curAlpha);
                    aTime = Time.time + dTime; //设置新一轮时间限制
                    Debug.Log(aTime);

                }
            }
        }
        else
        {
            i = 0;
        }
    }

}

[System.Serializable]
public class GoInfo
{
    public string ID;
    public int index;
    public MeshRenderer rend;
    public GameObject[] obj;
    public GameObject curObj;
    private Color co;

    public GoInfo(string id0, int index0, GameObject obj0, MeshRenderer rend0)
    {
        ID = id0;
        index = index0;
        curObj = obj0;
        rend = rend0;
        
    }

}

创建物体:

技术分享

【over】

 

【Unity】透明度渐变