在Unity3D中更改图像的Alpha值以创建发光效果(Changing Alpha value of image in Unity3D to create glowing effect)

编程入门 行业动态 更新时间:2024-10-26 00:18:45
在Unity3D中更改图像的Alpha值以创建发光效果(Changing Alpha value of image in Unity3D to create glowing effect)

我有两个图像:一个有发光的边框,一个没有。 我想通过淡入淡出发光图像来创建图像发光的效果。 我的代码:

private Image lightImage; private float alpha; // Use this for initialization void Start () { lightImage = GetComponent<Image>(); alpha = Time.deltaTime * 10; } void FixedUpdate() { // fade to transparent over 500ms. lightImage.CrossFadeAlpha(0.0f, alpha, false); // and back over 500ms. lightImage.CrossFadeAlpha(1.0f, alpha, false); }

我似乎无法让它像我想要的那样工作(随着时间的推移缓慢过渡。)它快速闪烁,但这不是理想的效果。

I have two images: one with a glowing border and one without. I want to create the effect of an image glowing by fading the glowing image in and out. My code:

private Image lightImage; private float alpha; // Use this for initialization void Start () { lightImage = GetComponent<Image>(); alpha = Time.deltaTime * 10; } void FixedUpdate() { // fade to transparent over 500ms. lightImage.CrossFadeAlpha(0.0f, alpha, false); // and back over 500ms. lightImage.CrossFadeAlpha(1.0f, alpha, false); }

I cannot seem to get it to work like I want (a slow transition over time.) It blinks in and out quickly but that is not the desired effect.

最满意答案

替换: alpha = Time.deltaTime * 10; alpha = 0.5f; 而且我认为你应该将alpha变量重命名为duration 。 您不需要将CrossFadeAlpha放在FixedUpdate() 。 这是一个补间。 它不需要跨多个帧执行。 把它放在Start() 。 将alpha设置为绝对0并不是一个好主意,因为有时Unity会将0个alpha对象视为已禁用,但我不确定。 如果您遇到此问题,请将alpha设置为非常接近0的值(例如0.004f )。

如果你希望它淡出然后淡入,你应该这样做:

IEnumerator Fade() { // fade to transparent over 500ms. lightImage.CrossFadeAlpha(0.004f, 0.5f, false); // Wait for 500ms yield return new WaitForSeconds(0.5f); // and back over 500ms. lightImage.CrossFadeAlpha(1.0f, 0.5f, false); } void Start() { StartCoroutine(Fade()); } Replace: alpha = Time.deltaTime * 10; with alpha = 0.5f; And I think you should rename alpha variable to duration. You do not need to put CrossFadeAlpha in a FixedUpdate(). It is a tween. It does not need to be executed across multiple frames. Just put it in Start(). Setting alpha to absolute 0 is not a good idea because sometimes, Unity treats 0 alpha objects as if they were disabled but I'm not sure. If you do run into this problem, set the alpha to a value very closed to 0 instead (0.004f for example).

If you want it to fade out and then fade in, you should do it like this:

IEnumerator Fade() { // fade to transparent over 500ms. lightImage.CrossFadeAlpha(0.004f, 0.5f, false); // Wait for 500ms yield return new WaitForSeconds(0.5f); // and back over 500ms. lightImage.CrossFadeAlpha(1.0f, 0.5f, false); } void Start() { StartCoroutine(Fade()); }

更多推荐

本文发布于:2023-08-07 15:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   效果   Alpha   Unity3D   Changing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!