我试着用iTween.FadeTo在NGUI雪碧中做淡出效果,但不起作用。就像这样:
iTween.FadeTo(gOFlag, iTween.Hash("alpha",1.0f,"time",6f));我做错什么了吗?如果有,请告诉我。
发布于 2014-08-08 00:21:54
iTween使用Unity组件来淡化事物,但是NGUI UISprites没有iTween可以轻松访问的呈现器(它们可能甚至没有所有的渲染器,但是我必须研究NGUI源代码来确认这一点)。
今天早些时候,我遇到了同样的问题,并为此做了些事情:D
步骤0:如果您不知道C#中的扩展方法,请查看这段来自Prime31的精彩视频。如果您熟悉扩展方法,只需跳到步骤1 :p
步骤1:创建一个名为ExtensionMethods的新脚本。这将不是一个单行为,而是一个正常的静态类。
步骤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:在FadeIn或FadeOut上调用UISprite。请查看以下示例:
// 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业务是如何工作的?查看有关动作的来自Prime31的另一段很棒的视频。
https://stackoverflow.com/questions/24115473
复制相似问题