Unity实现物体逐渐消失(逐渐出现)

原理很简单,就是通过改变目标物体的Alpha值。但是要求材质的类型是Transparent/Diffuse

有用的地方在代码中标注了,直接贴代码

using UnityEngine;
using System.Collections;

public class Fade : MonoBehaviour
{

    float tempTime;
    void Start()
    {
        tempTime = 0;

        //获取材质本来的属性
        this.GetComponent().material.color = new Color(
        this.GetComponent().material.color.r,
        this.GetComponent().material.color.g,
        this.GetComponent().material.color.b, 
        //需要改的就是这个属性:Alpha值
        this.GetComponent().material.color.a);
    }
    void Update()
    {
        if (tempTime < 1)
        {
            tempTime = tempTime + Time.deltaTime;
        }
        if (this.GetComponent().material.color.a <= 1)
        {
            this.GetComponent().material.color = new Color(
            this.GetComponent().material.color.r
            , this.GetComponent().material.color.g,
            this.GetComponent().material.color.b,

            //减小Alpha值
            gameObject.GetComponent().material.color.a - tempTime / 50);
        }
    }
}

逐渐出现和这个类似,就是一开始Alpha值设置为0,然后逐渐增加

你可能感兴趣的:(unity3d)