创建一个协程来淡出不同类型的对象

嗨,我正在努力创建一个统一的协程,它将处理各种对象的淡化。 到目前为止,我能够获得我想要的不同类型的alpha值,但是如何在获取它之后设置新颜色我处于死路一条。 这是我的代码:

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().color.a; type = fadableComponent.GetType(); Debug.Log("Found a image"); break; } if (c.GetType() == typeof(Renderer)) { fadableComponent = c; alpha = fadableComponent.GetComponent().material.color.a; type = fadableComponent.GetType(); break; } if (c.GetType() == typeof(SpriteRenderer)) { fadableComponent = c; alpha = fadableComponent.GetComponent().color.a; type = fadableComponent.GetType(); break; } if(c.GetType()== typeof(CanvasGroup)) { fadableComponent = c; alpha = fadableComponent.GetComponent().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或什么不是,但我想避免这种情况。 例如我想避免的解决方案,我看了一下iTween的实现。 这是我想要避免的一个完美的例子:

 if(target.GetComponent()){ target.GetComponent().color=colors[3]; }else if(target.GetComponent()){ target.GetComponent().material.color=colors[3]; }else if(target.GetComponent()){ target.GetComponent().material.color=colors[3]; }else if(target.GetComponent()){ target.GetComponent().color=colors[3]; } 

这是一团糟,妈妈意大利面。 延长这将是一场噩梦。

它可能是你在寻找

Tweeng

“所有Unity中最令人惊奇的东西”。

这是游戏编程的可卡因。

基本语法是

  time.Tweeng( do something ) 

所以(伪代码)

 StartCoroutine( 2f.Tweeng( .. move the ball .. ) ); StartCoroutine( .5f.Tweeng( .. spin the spaceship .. ) ); StartCoroutine( 1f.Tweeng( .. fade the text .. ) ); 

这些例子是,

  • 将球移过两秒钟
  • 在1/2秒内旋转飞船
  • 将文本淡出一秒钟。

使用Tweeng是如此欣快,除非你有稳定的头脑,否则它会导致永久性的心理问题。

 // 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)=>{ca=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) ); 

你可以Tweeng任何东西,当然包括淡入淡出。

Tweeng的基本代码库

(实际上我个人喜欢在我的扩展文件中为每种类型提供一些不同的 Tweengs;generics方法很可爱但可能太聪明。重要的是要意识到这对你来说是不透明的;在你的代码库中你只是“Tweeng” “不关心这种类型,这很棒。简而言之,

,在您的扩展文件中,为每个与您相关的类型编写Tweeng。 然后,您可以在所有代码库中实现问题的目标。 只是吐温什么的。

请注意,您现在已经涵盖了各种动画; 不只是在你的问题中提到的淡出,而是每一个动作,旋转,反弹,缩放,效果等等。

注意陷入催眠状态! 🙂

实际上,这是一个扩展(使用Tweeng)我在某些情况下用于淡入淡出。 在lambda中设置变量以方便使用(例如示例中的“c”)很方便。

 public static void FadeIn(this Main mb, SpriteRenderer s) { Color c = s.color; ca = 0f; s.color = c; mb.StartCoroutine( 1.25f.Tweeng( (u)=>{ca=u;s.color=c;}, 0f,1f) ); } 

也!

与扩展和tweeng无关。 不要忘记最近添加到Unity的鲜为人知的crossFadeAlpha调用! 这个电话有点令人困惑,但它可以很好地工作。

这是一个例子(作为扩展!)

关键的事实是canvasRenderers在UI系统中总是具有alpha能力。

 public static class ExtensionsMeasures { public static void FadeIn(this Graphic g) { g.GetComponent().SetAlpha(0f); g.CrossFadeAlpha(1f,.15f,false); } public static void FadeOut(this Graphic g) { g.GetComponent().SetAlpha(1f); g.CrossFadeAlpha(0f,.15f,false); }