嗨,我正在尝试创建一个统一的协程,它将处理各种对象的淡化。到目前为止,我能够获得我想要的不同类型的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<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或什么不是,但我想避免这种情况。例如我想避免的解决方案我看了iTween实现。这是我想要避免的一个完美的例子:
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];
}
这是一团糟,妈妈意大利面。延长这将是一场噩梦。
答案 0 :(得分:5)
可能是你正在寻找
游戏编程的可卡因!
基本语法是
time.Tweeng( do something )
所以(伪代码)
StartCoroutine( 2f.Tweeng( .. move the ball .. ) );
StartCoroutine( .5f.Tweeng( .. spin the spaceship .. ) );
StartCoroutine( 1f.Tweeng( .. fade the text .. ) );
这些例子是,
更多...
// 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) );
你可以Tweeng任何东西,当然包括淡化。
Tweeng的基本代码库:
将它放在文件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, t) );
yield return null;
}
var(zz);
}
}
(如果您有兴趣,可以使用泛型方法 - QA on that)
Tweeng - 它可以拯救你的生命!
答案 1 :(得分:1)
关于此特定问题的仅供参考:
注意crossFadeAlpha
,最近添加到Unity中。这很令人困惑,但可以正常使用。请注意,画布渲染器始终始终具有Alpha。
public static class MoExtensions
{
public static void FadeIn(this Graphic g)
{
g.GetComponent<CanvasRenderer>().SetAlpha(0f);
g.CrossFadeAlpha(1f,.15f,false);
}
public static void FadeOut(this Graphic g)
{
g.GetComponent<CanvasRenderer>().SetAlpha(1f);
g.CrossFadeAlpha(0f,.15f,false);
}