创建一个协程淡出不同类型的对象 [英] Create a coroutine to fade out different types of object

查看:218
本文介绍了创建一个协程淡出不同类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想团结将要处理的对象的所有方式褪色创建一个协程。到目前为止,我能够得到不同类型的我想要的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;
            }
        }

现在我知道如何做到这一点可以说另一设置的,如果公司还是没有什么,但我想避免这种情况。比如解决方案,我想避免我接过一看与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.

推荐答案

这可能是你正在寻找

在所有团结的一个最令人惊讶的事情。

"the single most amazing thing in all of Unity".

这是破解游戏编程的可卡因。

It's the crack cocaine of game programming.

的基本语法是

 time.Tweeng( do something )

这样(以伪代码)

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



这些例子是,

Those examples are,


  • 将皮球可以在两秒内

  • 旋转飞船在1/2秒

  • 淡出文本超过一秒钟。

不要尝试使用Tweeng,除非你真的有一个内置的感受软件!

Do not try to use Tweeng unless you really have an in-built feel for software!

使用Tweeng是如此欣快它可能会导致永久性的心理问题,除非你有一个很好的头编程。

Using Tweeng is so euphoric it can cause permanent psychological problems unless you have an excellent head for programing.

// 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任何东西,当然包括变淡。

You can Tweeng anything, certainly including fades.

基本代码库Tweeng

(其实我个人喜欢只是,在我的扩展文件,有几个不同的的Tweengs每种类型,泛型方法是可爱的,但也许是太聪明了。意识到是不透明的,你是很重要的;在你的代码库,你只是Tweeng,没有关心的类型,这是伟大总之,

(Actually personally I like to just have, in my Extensions file, a few different Tweengs for each type; the generics approach is cute but maybe too clever. It's important to realize that is opaque to you; in your code base you just "Tweeng" with no concern for the type, which is great. In short,

,在您的扩展文件,写Tweeng为每种类型的,是有关你,你就可以实现你的问题的确切目的在所有的代码库。只是吐温什么。

, in your extensions file, write a Tweeng for each type that is relevant to you. You can then achieve exactly the aim of your question in all your code base. Just Tween anything.

请注意,您现在已经覆盖每个类的动画,不只是变淡在你的问题中提到,但每个 移动,旋转,弹跳,变焦效果,等等等等。

Note that you have now covered every sort of animation; not just fades as mentioned in your question but every move, spin, bounce, zoom, effect, etc etc.

当心坠落到一个催眠状态! :)

Watch out for falling in to a hypnotic state! :)

事实上,这里是一个扩展(使用Tweeng)我在某些情况下使用变淡。它的方便,以设置为拉姆达内方便使用(如在例如C)的变量。

Indeed, here's an extension (using Tweeng) I use in some cases for fades. It's convenient to set up variables for handy use inside the lambda (such as "c" in the example).

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






也!



无关的扩展和tweeng。不要忘了最近添加到Unity鲜为人知的 crossFadeAlpha 的来电!电话是一个有点混乱,但它可以很好地工作。


Also!

Unrelated to extensions and tweeng. Don't forget the little-known crossFadeAlpha call recently added to Unity! The call is a little confusing but it can work very well.

下面是一个例子(显示为一个扩展!)

Here's an example (presented as an extension!)

关键事实是, canvasRenderers 总有一个alpha能力,在UI系统。

The critical fact is that canvasRenderers always have an alpha ability, in the UI system.

public static class ExtensionsMeasures
    {

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

这篇关于创建一个协程淡出不同类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆