unity 渐变消失_【Unity】透明度渐变

本文介绍了一个Unity3D脚本,用于实现物体透明度的渐变效果。通过`TransEffect`类,控制多个物体依次进行渐变消失和出现,调整`curAlpha`和`nextAlpha`实现透明度变化,并利用`Time.deltaTime`控制速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;public classTransEffect : MonoBehaviour

{public ListGoList;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 voidOnEnable()

{

LoadGo();

}//Use this for initialization

voidStart()

{//初始化全List隐形

foreach (GoInfo go inGoList)

{

Color c=go.rend.material.color;

c.a= 0;

go.rend.material.color=c;

}

}//Update is called once per frame

public voidUpdate()

{

Trans();

}voidLoadGo()

{

GoList= new List();

GoList.Add(new GoInfo("Cylinder", 0, transform.Find("Cylinder").GetComponent(), transform.Find("Cylinder").GetComponent()));

GoList.Add(new GoInfo("Cube", 1, transform.Find("Cube").GetComponent(), transform.Find("Cube").GetComponent()));

GoList.Add(new GoInfo("Sphere", 2, transform.Find("Sphere").GetComponent(), transform.Find("Sphere").GetComponent()));

GoList.Add(new GoInfo("Capsule", 3, transform.Find("Capsule").GetComponent(), transform.Find("Capsule").GetComponent()));

}private voidTrans()

{

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 classGoInfo

{public stringID;public intindex;publicMeshRenderer rend;publicGameObject[] obj;publicGameObject curObj;privateColor co;public GoInfo(string id0, intindex0, GameObject obj0, MeshRenderer rend0)

{

ID=id0;

index=index0;

curObj=obj0;

rend=rend0;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值