创建协程淡出不同类型的对象 [英] Create a coroutine to fade out different types of object
问题描述
我正在尝试创建一个统一的协程,它将处理各种对象的淡入淡出.到目前为止,我能够获得我想要的不同类型的 alpha 值,但是对于如何在 lerping 之后设置新颜色,我处于死胡同.这是我的代码:
Hi I'm trying to create a coroutine in unity that will handle fading on all manner of object. So far I'm able to get the alpha values of the different types I want but I'm at a dead end as to how to set the new color after lerping it. Here is my code :
public static IEnumerator FadeTo(float aValue, float aTime,GameObject gameObj)
{
Component[] allComponents = gameObj.GetComponents(typeof(Component));
Component fadableComponent;
Type type = null;
float alpha=0;
foreach(Component c in allComponents)
{
if(c.GetType()==typeof(Image))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<Image>().color.a;
type = fadableComponent.GetType();
Debug.Log("Found a image");
break;
}
if (c.GetType() == typeof(Renderer))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<Renderer>().material.color.a;
type = fadableComponent.GetType();
break;
}
if (c.GetType() == typeof(SpriteRenderer))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<SpriteRenderer>().color.a;
type = fadableComponent.GetType();
break;
}
if(c.GetType()== typeof(CanvasGroup))
{
fadableComponent = c;
alpha = fadableComponent.GetComponent<CanvasGroup>().alpha;
type = fadableComponent.GetType();
}
Debug.Log(alpha.ToString());
}
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, t));
gameObj.transform.GetComponent(type) // What now ?!
yield return null;
}
yield return null;
}
}
现在我知道如何做到这一点,让我们说另一组 if 或 what not,但我想避免这种情况.例如,我想避免的解决方案我查看了 iTween 实现.这是我想要避免的完美示例:
Now i know how to do this with lets say another set of if's or what not, but I want to avoid that. For example of solutions I want to avoid I took a look at the iTween implementation. Here is a perfect example of what I want to avoid:
if(target.GetComponent<GUITexture>()){
target.GetComponent<GUITexture>().color=colors[3];
}else if(target.GetComponent<GUIText>()){
target.GetComponent<GUIText>().material.color=colors[3];
}else if(target.GetComponent<Renderer>()){
target.GetComponent<Renderer>().material.color=colors[3];
}else if(target.GetComponent<Light>()){
target.GetComponent<Light>().color=colors[3];
}
这是一团糟,妈妈们的意大利面.延长这将是一场噩梦.
This is a mess, moms spaghetti. Extending this will be a nightmare.
推荐答案
这可能是您正在寻找的
游戏编程的可卡因!
基本语法是
time.Tweeng( do something )
so(伪代码)
StartCoroutine( 2f.Tweeng( .. move the ball .. ) );
StartCoroutine( .5f.Tweeng( .. spin the spaceship .. ) );
StartCoroutine( 1f.Tweeng( .. fade the text .. ) );
这些例子是,
- 将球移动超过两秒
- 在 1/2 秒内旋转飞船
- 在一秒钟内淡化文本.
更多...
// tweeng z to 20 degrees in .12 seconds
StartCoroutine(.12f.Tweeng( (t)=>transform.Eulers(0f,0f,t), 0f,20f) );
// fade in alpha in .75 seconds
StartCoroutine(.75f.Tweeng( (u)=>{c.a=u;s.color=c;}, 0f,1f) );
// duck volume..
StartCoroutine( .3f.Tweeng( x=>{current.volume=x;}, current.volume, 0f) );
// move something..
StartCoroutine(secs.Tweeng( (p)=>parked.transform.position=p,
parked.transform.position,
landing.transform.position) );
// twist image
yield return StartCoroutine( .3f.Tweeng(
(u)=>polaroid.localEulerAngles = new Vector3(0f,0f,u),
0f,9f) );
您可以吐温任何东西,当然包括淡入淡出.
You can Tweeng anything, certainly including fades.
Tweeng 的基本代码库:
Basic code base for Tweeng :
把它放在一个文件 Extns.cs 中:
Just put this in a file Extns.cs:
public static class Extns
{
public static IEnumerator Tweeng( this float duration,
System.Action<float> var, float aa, float zz )
{
float sT = Time.time;
float eT = sT + duration;
while (Time.time < eT)
{
float t = (Time.time-sT)/duration;
var( Mathf.SmoothStep(aa, zz, t) );
yield return null;
}
var(zz);
}
public static IEnumerator Tweeng( this float duration,
System.Action<Vector3> var, Vector3 aa, Vector3 zz )
{
float sT = Time.time;
float eT = sT + duration;
while (Time.time < eT)
{
float t = (Time.time-sT)/duration;
var( Vector3.Lerp(aa, zz, Mathf.SmoothStep(0f, 1f, t) );
yield return null;
}
var(zz);
}
}
(请注意,这些示例包括平滑.在某些情况下您可能不需要平滑.实际上,现在在项目中我们总是同时拥有Tweeng"和SmoothedTweeng".)
(Note that those examples include smoothing. You may need no-smoothing in some cases. In fact, in projects now we always have both "Tweeng" and "SmoothedTweeng".)
(如果您有兴趣,可以使用泛型方法 - 引人入胜的 对此进行质量检查.)
(If you're interested, you can use a generics approach - fascinating QA on that.)
吐温 - 它可以挽救您的生命!
Tweeng - it could save your life!
这篇关于创建协程淡出不同类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!