淡出Unity UI文本

时间:2015-01-11 08:13:24

标签: c# unity3d unity3d-gui

当我在UI文本上运行以下代码时

Color color = text.color;
color.a -= 1.0f;
text.color = color;

文本的alpha值立即设置为0.我怎样才能简单地淡出文本。

7 个答案:

答案 0 :(得分:12)

如果您使用的是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);

这两个功能使用起来更好一些,因为您不必担心跟踪任何事情。只需打电话就可以开始新的一天。

答案 1 :(得分:3)

您可以使用协同程序:

示例:

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

答案 2 :(得分:2)

Unity中的

Color值工作在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;

答案 3 :(得分:2)

将此添加到更新方法或协程 -

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

答案 4 :(得分:0)

如果您使用文本对象,这是我更简单的解决方案。代码淡化Text对象的文本,它附加到其中。可以使用blinkStep更改速度。 (为了测试,只需公开)。您只需将其复制并粘贴到名为“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
    }
}

答案 5 :(得分: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;
    }

}

答案 6 :(得分: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);
    }
}