Lerp 颜色的 alpha 值
Lerp the alpha value of a color
我想来回更改颜色的 alpha 值(透明度):
private void Awake()
{
material = gameObject.GetComponent<Renderer>().material;
oColor = material.color; // the alpha here is 0
nColor = material.color;
nColor.a = 255f;
}
private void Update()
{
material.color = Color.Lerp(oColor, nColor, Mathf.PingPong(Time.time, 1));
}
这个不行,颜色瞬间变白,一直闪烁原来的颜色。我正在关注 this.
实际上 Material
的 alpha
颜色从 0 到 255,但是这些值在 C#
中从 0 翻译成 1,使 1 等于 255。这是我前段时间制作的脚本,也许你可以使用它:
public class Blink : MonoBehaviour
{
public Material m;
private Color c;
private float a = .5f;
private bool goingUp;
private void Start()
{
c = m.color;
goingUp = true;
}
private void Update()
{
c.a = goingUp ? .03f + c.a : c.a - .03f;
if (c.a >= 1f)
goingUp = false;
else if (c.a <= 00)
goingUp = true;
m.color = c;
}
}
你可以试试这个:
void FixedUpdate ()
{
material.color = new Color (0f, 0.5f, 1f, Mathf.PingPong(Time.time, 1));
}
这里的颜色是蓝色,(RGB 0, 128, 255) "Mathf.PingPong(Time.time, 1) 处理 alpha。
当您在检查器中将 material "rendering mode" 设置为 "transparent" 时,效果完全可见:)
注:
使用"transparent"渲染模式将使对象在alpha层下降到0时透明,
使用"fade"渲染模式将使对象在alpha层下降到0时完全不可见。
我想来回更改颜色的 alpha 值(透明度):
private void Awake()
{
material = gameObject.GetComponent<Renderer>().material;
oColor = material.color; // the alpha here is 0
nColor = material.color;
nColor.a = 255f;
}
private void Update()
{
material.color = Color.Lerp(oColor, nColor, Mathf.PingPong(Time.time, 1));
}
这个不行,颜色瞬间变白,一直闪烁原来的颜色。我正在关注 this.
实际上 Material
的 alpha
颜色从 0 到 255,但是这些值在 C#
中从 0 翻译成 1,使 1 等于 255。这是我前段时间制作的脚本,也许你可以使用它:
public class Blink : MonoBehaviour
{
public Material m;
private Color c;
private float a = .5f;
private bool goingUp;
private void Start()
{
c = m.color;
goingUp = true;
}
private void Update()
{
c.a = goingUp ? .03f + c.a : c.a - .03f;
if (c.a >= 1f)
goingUp = false;
else if (c.a <= 00)
goingUp = true;
m.color = c;
}
}
你可以试试这个:
void FixedUpdate ()
{
material.color = new Color (0f, 0.5f, 1f, Mathf.PingPong(Time.time, 1));
}
这里的颜色是蓝色,(RGB 0, 128, 255) "Mathf.PingPong(Time.time, 1) 处理 alpha。
当您在检查器中将 material "rendering mode" 设置为 "transparent" 时,效果完全可见:)
注:
使用"transparent"渲染模式将使对象在alpha层下降到0时透明,
使用"fade"渲染模式将使对象在alpha层下降到0时完全不可见。