[英]Fade out Unity UI text
當我在UI文本上運行以下代碼時
Color color = text.color;
color.a -= 1.0f;
text.color = color;
文本的alpha值立即設置為0.我怎樣才能簡單地淡出文本。
如果您使用的是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);
如果您使用文本對象,這是我更簡單的解決方案。 代碼淡化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
}
}
這是任何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;
}
}
對於每個人,您可以將此腳本用作每個文本的組件:
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.