【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】

时间: 2024-08-03 18:08:45

【Unity】透明度渐变的相关文章

code实现透明度渐变和颜色渐变的view

最近用code写了些view上的渐变效果,使得app的UI特效不必全部依赖美工出的图片. 效果如下: 主要用到了Layer上的渐变层,核心代码如下,关于CALaer的使用可参考 //Transparent Gradient Layer - (void) insertTransparentGradient { UIColor *colorOne = [UIColor colorWithRed:(33/255.0) green:(33/255.0) blue:(33/255.0) alpha:0.0

android动画的透明度渐变、旋转动画、缩放动画、评议动画

这是我在学习android的时候做的一个小小的东西可以实现图片的旋转.平移.缩放.透明度的渐变 首先我们要创建一个android的项目 在自己的drawable-mdpi中添加自己的图片 然后在res目录中,创建一个名称是anim(动画)的目录,并且在该目录中实现图片的操作 首先是anim_alpha.xml定义一个实现透明渐变的动画该动画实现的是完全不透明-->完全透明---->完全不透明 <pre name="code" class="html"

js函数封装,使一个obj的透明度渐变,淡入淡出(兼容IE)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

JS实现透明度渐变动画

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS实现透明度渐变动画</title> <style type="text/css"> *{margin:0;padding:0;} #box{width:200px;height:200px;background-color

view渐变色,透明度渐变

1 功能描述 开发中经常遇到这样的需求:view2显示在view1上面,透过view2可以渐渐的看到view1.效果如图1所示:view1是一个imageView,view2是一个普通view.view1与view2完全重叠,view2从下到上由灰色到白色,由不透明到透明.图2为只有view2的效果,图3为只有view1的效果. 2 代码如下 如果只想颜色渐变,而不需要透明度的渐变,可以将alpha设为1.0. 1 - (void)viewDidLoad { 2 [super viewDidLo

20150620图片的透明度渐变

function fade(obj,tar) { obj.timer && clearInterval(obj.timer); obj.timer=setInterval(function () { var cur=parseInt(css(obj,'opacity')*100); var speed=(tar-cur)/7; speed=(speed>0)?Math.ceil(speed):Math.floor(speed); if(cur!=tar){ obj.style.opa

android透明度渐变动画

AlphaAnimation anim01 = new AlphaAnimation(02.f, 0.1f); //动画持续的时间 anim01.setDuration(500); //设置动画持续的次数 anim01.setRepeatCount(AlphaAnimation.INFINITE); //应该做什么当它到达尽头.应用此设置只在重复计数大于0或无限.默认为重启. anim01.setRepeatMode(AlphaAnimation.REVERSE); //开启动画 mArrpw0

android游戏开发5-10 补间动画 透明度渐变 旋转 缩放 平移

首先在res文件下新建anim文件夹 新建anim_alpha.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="2000" android:fillAfter="

css3颜色+透明度渐变

.linear { width: 630px; height: 120px; line-height: 150px; text-align: center; font-size: 26px; position: absolute; bottom: 0; color: #fff; background: -webkit-linear-gradient( top, rgba(0, 0, 0, 0),rgba(0, 0, 0, 0.2) ); /* Safari 5.1 - 6 */ backgrou