IEnumerator CoroutineFadeTo(AnimationType animationType, Transform trans, Vector3 endValueS, float timeS,float timeB)
{
Vector3 endValue=endValueS;
Vector3 startValue = Vector3.zero;
Vector3 startValueS = Vector3.zero;
bool isEndValue = false;
float time = 0;
switch (animationType)
{
case AnimationType.Move:
startValue = trans.position;
break;
case AnimationType.LocalMove:
startValue = trans.localPosition;
break;
case AnimationType.Rotete:
startValue = trans.eulerAngles;
break;
case AnimationType.LocalRotate:
startValue = trans.localEulerAngles;
break;
case AnimationType.Scale:
startValue = trans.localScale;
break;
default:
break;
}
startValueS = startValue;
p: ;
float dis=Vector3.Distance(startValue, endValue); ;
switch (animationType)
{
case AnimationType.Move:
dis = Vector3.Distance(trans.position, endValue);
break;
case AnimationType.LocalMove:
dis = Vector3.Distance(trans.localPosition, endValue);
break;
case AnimationType.Rotete:
dis = Vector3.Distance(trans.eulerAngles, endValue);
break;
case AnimationType.LocalRotate:
dis = Vector3.Distance(trans.localEulerAngles, endValue);
break;
case AnimationType.Scale:
dis = Vector3.Distance(trans.localScale, endValue);
break;
default:
break;
}
if (dis <= 0.001f)
{
if (isEndValue)
{
endValue = endValueS;
isEndValue = false;
time = timeB;
}
else {
endValue = startValue;
isEndValue = true;
time = timeS;
}
goto p;
}
if (time==0)
{
time = timeS;
}
//posOffset
float xoffset = (endValue.x - startValue.x) / (time / 0.02f);
float yoffset = (endValue.y - startValue.y) / (time / 0.02f);
float zoffset = (endValue.z - startValue.z) / (time / 0.02f);
while (true)
{
yield return new WaitForFixedUpdate();
switch (animationType)
{
case AnimationType.Move:
trans.position += new Vector3(xoffset, yoffset, zoffset);
if (Vector3.Distance(trans.position,endValue)<0.01f)
time = -1;
break;
case AnimationType.LocalMove:
trans.localPosition += new Vector3(xoffset, yoffset, zoffset);
if (Vector3.Distance(trans.localPosition, endValue) < 0.01f)
time = -1;
break;
case AnimationType.Rotete:
trans.eulerAngles += new Vector3(xoffset, yoffset, zoffset);
if (Vector3.Distance(trans.eulerAngles, endValue) < 0.01f)
time = -1;
break;
case AnimationType.LocalRotate:
trans.localEulerAngles += new Vector3(xoffset, yoffset, zoffset);
if (Vector3.Distance(trans.localEulerAngles, endValue) < 0.01f)
time = -1;
break;
case AnimationType.Scale:
trans.localScale += new Vector3(xoffset, yoffset, zoffset);
if (Vector3.Distance(trans.localScale, endValue) < 0.01f)
time = -1;
break;
default:
break;
}
if (time <= 0)
{
if (isEndValue)
{
endValue = endValueS;
startValue = startValueS;
isEndValue = false;
time = timeB;
audioFade.Play();
}
else
{
endValue = startValue;
startValue = endValueS;
isEndValue = true;
time = timeS;
}
goto p;
}
}
}
此段代码主要是实现简单的pingpong类型的渐变动画