SoFunction
Updated on 2025-03-06

Unity3D realizes standby state picture loop fade in and out

This article shares the specific code for Unity3D to realize the fading of image loops for your reference. The specific content is as follows

1. Description

Due to recent project requirements, I found the code for fading in and out on the Internet, but the two pictures disappeared after fading in and out (maybe I didn’t find other good works). So I made a simple extension

2. Simple ideas

Since the picture fades in and fades in standby state, first, we must determine when it is standby (that is, when the screen does not operate); second, after the picture is still for a period of time, it starts to fade, the first picture fades in and the second picture fades out; then the picture is still for a period of time, and then the next fades out, but because it is a loop fade, we must consider reloading the first photo (the next fades out, the second photo must be reloaded). In the fading cycle, you should also consider the loop of the picture alpha value from 1 to 0 and then from 0 to 1 (it can avoid flickering and fading, which makes you feel natural); finally, it is determined that you enter a non-standby state, that is, there is an operation.

3. Code implementation and analysis

Introduce UI namespace and use UI to fade in and out effect;

using ;

public Image nobody_Img1;
public Image nobody_Img2;//Two pictures of fading in and outpublic float fadeTotalTime=5f;//Fail timepublic float imageStaticTime=3f;//Picture still timepublic float runningTime=10f;//Program run timepublic int StandbyTime=5;//No operation time/*
[HideInInspector]
public bool standby=false;
*/

private bool standby=false;//Whether it is in standby modeprivate float startTime=0;//Start standby timeprivate int imgIndex=2;//Picture index (picture name)private float remainder=0//Next standby start timeprivate bool hasStartNext=false;//Have the next standby picture loadedprivate bool canLoad=true;//Is the picture possible to loadprivate bool startCountTime=false;//Can you count the standby timeprivate int standbyTime=0;//Standby timeprivate int time=0;//Frame count, used to count standby timeprivate Vector3 prevMousePos=;//The position of the previous frame of the mouse is
/*Variable Description
 After judging that the screen has no operation (and the mouse position must not change), start counting the time when there is no operation (i.e. startCountTime=true, time++ (in the FixedUpdate function), standbyTime++), when standbyTime exceeds the specified time, standby=true; start the picture fading in and out
 */

/*
 When the program is just running, regardless of whether there is any operation or not, the startup time will start counting ten seconds.
 */
IEnumerator StartPrepareStandby()
{
 yield return new WaitForSeconds(runningTime);
 startCountTime=true;//Start counting the standby time}

/*
 After entering standby, two pictures are displayed and faded in and out in the loop after a period of time.
 */
IEnumerator StartFirstFade()
{
 //Two real pictures nobody_Img1.enabled=true;
 nobody_Img2.enabled=true;
 yield return new WaitForSeconds(imageStaticTime);
 //Reset time startTime=;//The start standby time is equal to the current time of the program remainder=startTime;//Record the time when fading starts //Start standby standby=true;
}

/*
 The fade cycle after the first fade is started
 */
IEnumerator StartNextFade()
{
 if(imgIndex>=4)//Judge whether the image index is out of bounds (the image index is also the image name)  imgIindex=0;
 // canLoad is used here to determine which image to load if(canLoad)
 {
  nobody_Img1.sprite=((),typeof(Sprite)) as Sprite;
 }
 else
 {
  nobody_Img2.sprite=((),typeof(Sprite)) as Sprite;
 }
 canLoad = !canLoad;//Inverse, used to distinguish the loading of pictures imgIndex++;//The image index is accumulated, and the next picture is loaded next time yield return new WaitForSeconds(imageStaticTime);
 //Reset the fade time startTime=;
 remainder=startTime;
 //The picture has been loaded, waiting for the next loading hasStartNext=false;
}

void Start()
{
 //Call the coroutine that starts counting the standby time StartCoroutine(StartPrepareStandby());
}

void FixedUpdate()
{
 if(startCountTime)
 {//Stat time without operation  if(==prevMousePos)
  {//Judge whether the mouse is still moving   time++;
  }
  else
  {//Reset standby time when the mouse is moved   standbyTime=0;
   time=0;
  }
 }
 if(time>=50)
 {
  time=0;
  standbyTime++;//Standby seconds }
 if(standbyTime>StandbyTime)
 {//The standby state is considered to be exceeded if the specified time is not operated.  standbyTime--;//The first time the image fade is executed only once  startCountTime=false;// Statistics of stop standby time  StartCoroutine(StartFirstFade());//First the first picture fades in and out }
}

void Update()
{
 if((0))
 {//Every time the mouse is pressed, it stops standby and related judgments  StopCoroutine(StartNextFade());//Stop fading  standby=false;//Exit standby state  //Retain the alpha value of the current image  if(canLoad)
  {//Judge which photo is fading based on the current canLoad   nobody_Img1.color=new Color(1,1,1,1);
   nobody_Img2.color=new Color(1,1,1,0);
  }
  else
  {
   nobody_Img1.color=new Color(1,1,1,0);
   nobody_Img2.color=new Color(1,1,1,1);
  }
  //Hide standby picture   nobody_Img1.enabled=false;
   nobody_Img2.enabled=false;
   //Reset the standby time   standbyTime=0;
   time=0;
 }
 else if((0))
 {//Every time the mouse is lifted, it is considered to be no operation  startCountTime=true;
  prevMousePos=;
 }
 if(standby)
 {
  if(<startTime+fadeTotalTime)
  {
   float alphaValue=(-remainder)/fadeTotalTime;
   if(canLoad)
   {
    nobody_Img1.color=new Color(1,1,1,1-alphaValue);
    nobody_Img2.color=new Color(1,1,1,alphaValue);
   }
   else
   {
    nobody_Img1.color=new Color(1,1,1,alphaValue);
    nobody_Img2.color=new Color(1,1,1,1-alphaValue);
   }
  }
  else
  {
   if(!hasStartNext)
   {
    hasStartNext=true;//The next photo has started loading    StartCoroutine(StartNextFade());//Start the next fade coroutine   }
  }
 }
 prevMousePos=;//Record the mouse position of each frame}

Summarize

Although I feel that the idea is clear this time, it still takes a long time to complete this simple program; I also firmly believe that there must be simple methods for this simple program; I feel that my program is a bit confusing and cumbersome (for the extension of the program, you can also refer to the singleton mode of the design pattern or other mode for standby status judgment in other scripts). Welcome to give me some advice!

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.