【问题标题】:Create a coroutine to fade out different types of object创建协程淡出不同类型的对象
【发布时间】:2016-05-14 07:28:17
【问题描述】:

您好,我正在尝试创建一个统一的协程来处理各种对象的褪色。到目前为止,我能够获得我想要的不同类型的 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];   
        }

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

【问题讨论】:

  • Uri,您可能正在寻找 c# 语言中的 (1),generics,这是该语言非常强大的部分。 “即时介绍”超出了 QA 的范围,请谷歌获取教程。但是 (2) 在 Unity 中,我认为您正在寻找 Tweeng​​i>(“Unity 中最令人惊奇的事情”)...stackoverflow.com/questions/36121120/…跨度>
  • Uri,请注意,基本上你必须在这里使用扩展。 (无论您选择哪种解决方案。)如果是 Unity 新手 stackoverflow.com/a/35629303/294884

标签: c# unity3d


【解决方案1】:

你可能正在寻找

吐温

游戏编程的可卡因!

基本语法是

 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 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 的基本代码库:

只需将其放入 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”。)

(如果您有兴趣,可以使用泛型方法——令人着迷的QA on that。)

Tweeng - 它可以挽救你的生命!

【讨论】:

  • 一如既往的好答案。还有一个问题,这适用于 iOS 吗?
  • 我不得不说我刚刚在我的工具中实现了 Tweeng,它很漂亮。一滴男子汉的眼泪从我的脸颊滚落。
  • 不错。我编辑了矢量表格以进行平滑处理。
  • 嗨@Ruzihm!这是一个绝妙的主意,但请自己添加它作为单独的答案! (事实上​​,我自己总是有一个“Tween”和一个“SmoothedTweeng”)你摇滚
  • 实际上忽略了@Ruzihm,您这样做的方式更加一致!呵呵! :)
【解决方案2】:

关于这个特定问题的仅供参考:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2013-04-25
    相关资源
    最近更新 更多