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;
}
}