改变图片透明度

///模型从不透明变成透明直至消失
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestClass : MonoBehaviour
{
    private float tempTime;
    public GameObject StartBtn;
    bool _Check = false;
    Shader shader;

    void Start()
    {
        shader = Shader.Find("Transparent/Diffuse");
        this.GetComponent<Renderer>().material.shader=shader;
        StartBtn.GetComponent<Button>().onClick.AddListener(delegate
        {
            _Check = true;
        });
        tempTime = 0;
        //获取材质本来的属性  
        this.GetComponent<Renderer>().material.color = new Color
        (
                this.GetComponent<Renderer>().material.color.r,
                this.GetComponent<Renderer>().material.color.g,
                this.GetComponent<Renderer>().material.color.b,
                //需要改的就是这个属性:Alpha值  
                this.GetComponent<Renderer>().material.color.a
        );
    }
    void Update()
    {
        if (_Check)
        {
            if (tempTime < 1)
            {
                tempTime = tempTime + Time.deltaTime;
            }
            if (this.GetComponent<Renderer>().material.color.a <= 1)
            {
                this.GetComponent<Renderer>().material.color = new Color
                (
                    this.GetComponent<Renderer>().material.color.r,
                    this.GetComponent<Renderer>().material.color.g,
                    this.GetComponent<Renderer>().material.color.b,


                //减小Alpha值,从1-30秒逐渐淡化 ,数值越大淡化越慢 
                gameObject.GetComponent<Renderer>().material.color.a - tempTime / 20 * Time.deltaTime
                );
            }
            Destroy(this.gameObject, 20.0f);//40秒后消除
        }
    }
}
原文地址:https://www.cnblogs.com/Cocomo/p/7088089.html