When I run the following code on my UI text
Color color = text.color;
color.a -= 1.0f;
text.color = color;
The alpha value of the text is immediately set to 0. How can I simply fade out the text.
When I run the following code on my UI text
Color color = text.color;
color.a -= 1.0f;
text.color = color;
The alpha value of the text is immediately set to 0. How can I simply fade out the text.
如果您使用的是 Unity 4.6 和更新版本,您可以利用CrossFadeAlpha和CrossFadeColor。
例子:
// fade to transparent over 500ms.
text.CrossFadeAlpha(0.0f, 0.05f, false);
// and back over 500ms.
text.CrossFadeAlpha(1.0f, 0.05f, false);
这两个函数使用起来更好一些,因为您不必担心跟踪任何内容。打电话就可以开始你的一天了。
您可以使用协程:
例子:
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;
}
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;
将此添加到更新方法或协程中 -
if(text.color != Color.clear)
Color.Lerp (text.color, Color.clear, fadeSpeed * Time.deltaTime);
对于每个人,您都可以将此脚本用作每个文本的组件:
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);
}
}
这是任何 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;
}
}
如果您使用文本对象,这是我更简单的解决方案。代码淡入淡出它附加到的 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
}
}