目的:我想通過Color.Lerp(),將Canvas里的Text的顏色淡出,也就是將文字顏色的透明度平滑至0。
public Text m_Text;
void Update()
{
FadeText();
}
void FadeText()
{
m_Text.color = Color.Lerp(m_Text.color, new Color(255,0,0,0), 3f);
Debug.Log(m_Text.color);
}
我很不能理解為什么這個文字顏色是秒變透明度為0的,有知道的大牛幫助我嗎?
uj5u.com熱心網友回復:
謝邀Lerp函式使用的方法是正確的
但是問題出在這里m_Text.color = Color.Lerp(m_Text.color, new Color(255,0,0,0), 3f);
Lerp函式的第一個引數不應該是m_Text.color 因為m_Text.color 是被你設定成每幀改變,所以導致越來越快 時間不是3s
正確做法
//在最前面賦值
Color Begincolor = m_Text.color;
public Text m_Text;
void Update()
{
FadeText();
}
void FadeText()
{
m_Text.color = Color.Lerp(Begincolor , new Color(255,0,0,0), 3f);
Debug.Log(m_Text.color);
}
uj5u.com熱心網友回復:
大哥 你來結個帖子啊。。。uj5u.com熱心網友回復:
兩位對Lerp 沒理解。 第三個引數應該是 0~1 大于1則取1。Lerp(a,b,r)
其實 就是 a+(b-a)*r r如果是1 則不就是b了嘛, 瞬變。
你需要 用個 變數記錄
float changetime = 0f; 變化前設定為0
然后每次update 里 changetime+=Time.deltaTime;
m_Text.color = Color.Lerp(m_Text.color, new Color(255,0,0,0), changetime); //這個是不勻速,在1秒內變完。
如果想慢一點
m_Text.color = Color.Lerp(m_Text.color, new Color(255,0,0,0), changetime/3); //不勻速在3秒內變完。
uj5u.com熱心網友回復:
抱歉 確實是我想錯了 當時可能有點蒙 對于樓主 我誤答了 對樓主造成的錯誤和困擾我道歉
正確的因該是
public float duration = 3.0f;
float currentTime = 0;
float precent = 0;
Color Begincolor = m_Text.color;
public Text m_Text;
void update() {
currentTime += Time.deltaTime;
if(currentTime > duration )
currentTime = duration;
percent = currentTime / duration;
ChangeColor(percent);
}
void percent (float percent) {
m_Text.color = Color.Lerp(Begincolor , new Color(255,0,0,0), percent);
Debug.Log(m_Text.color);
}
這個應該是勻速3s變完
uj5u.com熱心網友回復:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TColor : MonoBehaviour {
public float duration = 3f;
float currentTime = 0;
public float percent = 0;
public Text m_Text;
Color a;
void Start () {
a = m_Text.color;
}
void Update () {
currentTime += Time.deltaTime;
if (currentTime > duration)
currentTime = duration;
percent = currentTime / duration;
ChangeColor (percent);
}
void ChangeColor (float percent) {
m_Text.color = Color.Lerp (a, Color.red, percent);
Debug.Log (m_Text.color);
}
}
這個試驗過了 可以 3秒勻變
層主
如果是你這樣寫
m_Text.color = Color.Lerp(m_Text.color, new Color(255,0,0,0), changetime/3);
Lerp函式第一個引數是m_Text 是不會3s的 是小于3s
uj5u.com熱心網友回復:
還有 樓主 你既然需要透明度 那就是RGBA紅色透明的RGBA是new Color(1,0,0,0)
而不是new Color(255,0,0,0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/61352.html
標籤:Unity3D
上一篇:unity游戲服務的撰寫
下一篇:類蛇蛇大作戰 相機跟隨玩家,怎么實作蛇移動時食物不會閃爍,平滑移動; 蛇蛇大作戰 怎么實作蛇移動時食物不會閃爍,平滑移動,那么多食物
