C# 淡出统一用户界面文本
当我在UI文本上运行以下代码时C# 淡出统一用户界面文本,c#,unity3d,unity3d-gui,C#,Unity3d,Unity3d Gui,当我在UI文本上运行以下代码时 Color color = text.color; color.a -= 1.0f; text.color = color; 文本的alpha值立即设置为0。如何简单地淡出文本。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; 将其添加到更
Color color = text.color;
color.a -= 1.0f;
text.color = color;
文本的alpha值立即设置为0。如何简单地淡出文本。Unity中的值在0f..1f
范围内工作,因此:
为0%(或编辑器中显示的0/255)0.0f
为50%(或127.5/255)0.5f
为100%(或255/255)1.0f
1.0f
将使值变为0%。尝试不同的减量,如0.1f
:
color.a -= 0.1f;
将其添加到更新方法或协同程序中-
if(text.color!=color.clear)
Color.Lerp(text.Color、Color.clear、fadeSpeed*Time.deltaTime)代码>您可以使用协同程序:
例如:
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;
}
// fade to transparent over 500ms.
text.CrossFadeAlpha(0.0f, 0.05f, false);
// and back over 500ms.
text.CrossFadeAlpha(1.0f, 0.05f, false);
公共文本;
公共无效淡出()
{
启动例行程序(淡出);
}
专用IEnumerator淡出器()
{
浮动持续时间=0.5f;//0.5秒
浮动电流时间=0f;
while(当前时间<持续时间)
{
float alpha=Mathf.Lerp(1f,0f,当前时间/持续时间);
text.color=新颜色(text.color.r,text.color.g,text.color.b,alpha);
currentTime+=Time.deltaTime;
收益返回空;
}
屈服断裂;
}
如果使用文本对象,下面是我的简单解决方案。代码将淡入和淡出文本对象所附着的文本。可使用闪烁步骤更改速度。(为了测试,只需将其公开)。您只需将其复制并粘贴到名为“TextFlacker”的脚本中,或将该类重命名为脚本的任何名称即可。;-)
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
公共类文本闪烁:单行为{
浮动闪烁持续时间秒=1f;
浮动闪烁进度=0f;
浮动闪烁步进=0.01f;
//Color txtColor=Color.black;
文本闪烁文本;
//用于初始化
无效开始(){
blinkingText=GetComponentParent();
}
//每帧调用一次更新
无效更新(){
如果((blinkProgress>1)|(blinkProgress如果您使用的是Unity 4.6及更新版本,则可以利用和
例如:
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;
}
// fade to transparent over 500ms.
text.CrossFadeAlpha(0.0f, 0.05f, false);
// and back over 500ms.
text.CrossFadeAlpha(1.0f, 0.05f, 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;
}
}
对于每个人,都可以将此脚本用作每个文本的组件:
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);
}
}
我是Unity的新手。这似乎有点低效。即使文本已完全褪色,您是否仍在继续更新每一帧中的文本颜色?您需要进一步解释,一些初学者将很难理解,例如何时何地编写这行代码。这里使用Lerp是错误的。最后一个参数应该是Lerp进度(0-1值)。请阅读关于lerp的文档。在Unity 4.6及更高版本的新UI库中,有一种非常简单的一行程序来实现这一点。我在下面为所有感兴趣的人提供了一个答案。