Home>

In Unity, how do we make the fading effect of the ui object glow fade out?

Let's say we have a picture of an elf with a moon halo

We can add a canvasgroup component to it

we can find out,The alpha value on the component can control the transparency of the picture,From 0 to 1

Then we can achieve the glowing halo effect by controlling the change of the alpha value loop in the code

Here is the code:

using system.collections;
using system.collections.generic;
using unityengine;
public class moonflash:monobehaviour {
 private canvasgroup mooncanvasgroup;
 private float flashspeed=0.2f;//Halo flash speed
 private bool ison=true;
 private float maxalpha=0.6f;//The highest alpha value displayed
 private float minalpha=0.05f;//The lowest alpha value displayed
 void start () {
 mooncanvasgroup=getcomponent<canvasgroup>();
 }
 void update () {
 if (mooncanvasgroup.alpha<maxalpha&&ison)
 {
  mooncanvasgroup.alpha +=flashspeed * time.deltatime;
 }
 else {
  ison=false;
  mooncanvasgroup.alpha-= flashspeed * time.deltatime;
  if (mooncanvasgroup.alpha<minalpha) {
  ison=true;
  }
 }
 }
}

Then mount the script on the picture,The moon glow effect is complete.

c
  • Previous The difference between java String, StringBuilder and StringBuffer
  • Next tensorboard implements displaying training and test curves simultaneously