淡出Unity UI文本

7
当我在我的UI文本上运行以下代码时:
Color color = text.color;
color.a -= 1.0f;
text.color = color;

文本的alpha值立即设置为0。我该如何简单地将文本淡出。


在Unity 4.6及以上版本中,使用新的UI库可以用一行超级简单的方法来完成这个操作。我已经为有兴趣的人提供了答案。 - Jonathan
7个回答

14
如果您正在使用Unity 4.6或更高版本,您可以利用CrossFadeAlphaCrossFadeColor
例如:
// fade to transparent over 500ms.
text.CrossFadeAlpha(0.0f, 0.05f, false);

// and back over 500ms.
text.CrossFadeAlpha(1.0f, 0.05f, false);

这两个函数使用起来更加方便,因为您不需要担心跟踪任何内容。只需调用它并继续进行您的工作即可。

4
你可以使用协程:
示例:
public Text text;
public void FadeOut()
{
    StartCoroutine(FadeOutCR);
}

private IEnumerator FadeOutCR()
{
    float duration = 0.5f; //0.5 secs
    float currentTime = 0f;
    while(currentTime < duration)
    {
        float alpha = Mathf.Lerp(1f, 0f, currentTime/duration);
        text.color = new Color(text.color.r, text.color.g, text.color.b, alpha);
        currentTime += Time.deltaTime;
        yield return null;
    }
    yield break;
}

3

Unity中的颜色值工作在0f..1f范围内,因此:

  • 0.0f是0%(或编辑器中显示的0/255)
  • 0.5f是50%(或127.5/255)
  • 1.0f是100%(或255/255)

减去1.0f将把该值变为0%。尝试使用不同的递减量,例如0.1f

color.a -= 0.1f;

1

将以下内容添加到update方法或协程中:

if(text.color != Color.clear) Color.Lerp (text.color, Color.clear, fadeSpeed * Time.deltaTime);


我是Unity的新手。这似乎有点低效。即使文本已经完全淡出,您仍在每帧更新文本的颜色吗? - Russell Horwood
你需要再解释得更详细一些,有些初学者可能会很难理解,比如何时何地编写这行代码。 - Amir Savand
这里使用Lerp的方式是错误的。最后一个参数应该是lerp进度(0-1值)。请阅读有关Lerp的文档。 - nipunasudha

0

这是任何UI文本或元素的闪烁代码。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Blink : MonoBehaviour {

    // this is the UI.Text or other UI element you want to toggle
    public MaskableGraphic imageToToggle;

    public float interval = 1f;
    public float startDelay = 0.5f;
    public bool currentState = true;
    public bool defaultState = true;
    bool isBlinking = false;


    void Start()
    {
        imageToToggle.enabled = defaultState;
        StartBlink();
    }

    public void StartBlink()
    {
        // do not invoke the blink twice - needed if you need to start the blink from an external object
        if (isBlinking)
            return;

        if (imageToToggle !=null)
        {
            isBlinking = true;
            InvokeRepeating("ToggleState", startDelay, interval);
        }
    }

    public void ToggleState()
    {
        imageToToggle.enabled = !imageToToggle.enabled;
    }

}

0

对于每个人来说,您都可以将此脚本用作每个文本的组件:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FadeController : MonoBehaviour
{
    public float fadeDuration = 0.5f;

    public float fadeDelay = 0f;

    public float fadeTo = 0f;

    public Text text;

    void Start ()
    {
        // Fade with initial delay
        Invoke ("fade", fadeDelay);
    }

    public void fade ()
    {
        // Fade in/out
        text.CrossFadeAlpha (fadeTo, fadeDuration, false);
    }
}

0

如果您使用文本对象,则以下是我更简单的解决方案。该代码会使附加到其中的文本对象的文本淡入淡出。可以使用blinkStep更改速度。(为了测试,只需将其设置为public)。您可以将其复制并粘贴到名为“TextFlicker”的脚本中,或将类重命名为您脚本的名称。;-)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TextFlicker : MonoBehaviour {
    float blinkDurationSecs =1f;
    float blinkProgress =0f;
    float blinkStep = 0.01f;
    //Color txtColor = Color.black;
    Text blinkingText;
    // Use this for initialization
    void Start () {
        blinkingText = GetComponentInParent<Text>();
    }

    // Update is called once per frame
    void Update () {
        if ((blinkProgress > 1)||(blinkProgress<0)) {
            blinkStep*=-1f;
        } 
        blinkProgress+=blinkStep;
        blinkingText.color = Color.Lerp (Color.black, Color.white, blinkProgress);// or whatever color you choose
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接