iTween.FadeTo不使用NGUI Sprite

时间:2014-06-09 07:19:10

标签: unity3d ngui itween

我试图使用iTween.FadeTo在NGUI的Sprite中淡出效果但不起作用。 那样:

iTween.FadeTo(gOFlag, iTween.Hash("alpha",1.0f,"time",6f));

如果我做错了,请告诉我。

1 个答案:

答案 0 :(得分:1)

iTween使用Unity Renderer组件来淡化内容,但是NGUI UISprites没有iTween可以轻松访问的渲染器(他们甚至可能都没有它们,我必须研究NGUI源来确认这一点)。

我实际上在今天早些时候遇到了同样的问题并为此做了一些事情:D

步骤0:如果您不了解C#中的扩展方法,请查看this great video from Prime31。如果您熟悉扩展方法,请跳到步骤1:p

步骤1:创建一个名为ExtensionMethods的新脚本。这不是Monobehaviour,而是普通的静态类。

第2步:将其粘贴到其中:

public static void FadeIn (this UIWidget uiWidget, float fadeTime, AnimationCurve fadeCurve, float startAlpha, System.Action onComplete)
{
    uiWidget.StartCoroutine(DoFadeIn(uiWidget, fadeTime, fadeCurve, startAlpha, onComplete));
}

static System.Collections.IEnumerator DoFadeIn (UIWidget uiWidget, float fadeTime, AnimationCurve fadeCurve, float startAlpha, System.Action onComplete)
{
    Color endCol = uiWidget.color;
    endCol.a = 1f;
    Color startCol = uiWidget.color;

    if (startAlpha >= 0)
    {
        startCol.a = startAlpha;
    }

    float fTimer = 0;
    while (fTimer < fadeTime)
    {
        fTimer += Time.deltaTime;
        uiWidget.color = Color.Lerp(startCol, endCol, fadeCurve.Evaluate(fTimer/fadeTime));
        yield return null;
    }

    if (onComplete != null)
    {
        onComplete();
    }
}

public static void FadeOut (this UIWidget uiWidget, float fadeTime, AnimationCurve fadeCurve, System.Action onComplete)
{
    uiWidget.StartCoroutine(DoFadeOut(uiWidget, fadeTime, fadeCurve, onComplete));
}

static System.Collections.IEnumerator DoFadeOut (UIWidget uiWidget, float fadeTime, AnimationCurve fadeCurve, System.Action onComplete)
{
    Color endCol = uiWidget.color;
    endCol.a = 0f;
    Color startCol = uiWidget.color;

    float fTimer = 0;
    while (fTimer < fadeTime)
    {
        fTimer += Time.deltaTime;
        uiWidget.color = Color.Lerp(startCol, endCol, fadeCurve.Evaluate(fTimer/fadeTime));
        yield return null;
    }

    if (onComplete != null)
    {
        onComplete();
    }
}

第3步:根据需要修改代码。也许你想传递像iTween这样的设定alpha值。

步骤4:在UISprite上调用FadeIn或FadeOut。请查看以下示例:

// Fill this by dragging the UISprite you want to Fade into the inspector
public UISprite uiSprite;

// Fade Time
public float fadeTime = 1f;

// The easing for the fade. Make sure you have a curve in the inspector or the fade will be instant / might break.
public AnimationCurve fadeCurve;

void FadeTest ()
{
    uiSprite.FadeIn(fadeTime, fadeCurve, 0f, OnFadeFinish);
}

void OnFadeFinish ()
{
    Debug.Log("Fade done!")
}

奖金步骤:不知道onComplete业务如何运作?查看有关操作的this other great video from Prime31