Unity3D 简单待机状态图片循环淡入淡出

来源:互联网 发布:淘宝客服骂人 编辑:程序博客网 时间:2024/05/21 10:23
  1. 说明
    由于近期项目需求,在网上找了淡入淡出的代码,但都是两张图片淡入淡出结束后就没有了(可能我没找到其他好的作品)。所以我做了简单的扩展

  2. 简单思路
    既然待机状态下的图片淡入淡出切换,所以,首先要判断何时为待机状态(即屏幕没有任何操作的情况下);其次,图片静止一段时间后,开始淡入淡出,第一张图片淡入,第二张淡出;接着图片再次静止一段时间,然后接着下次的淡入淡出,但因为是循环淡入淡出,所以要考虑重新加载第一张照片(再下一次淡入淡出要重新加载第二张照片)。还有在淡入淡出的循环中还要考虑图片alpha值从1到0然后从0到1的循环(可以避免闪烁淡入淡出,有种自然的感觉);最后判断进入非待机状态,即有操作。

  3. 代码实现及分析
    引入UI命名空间,用UI做淡入淡出效果;
    using UnityEngine.UI;

public Image nobody_Img1;public Image nobody_Img2;//淡入淡出的两张图片public float fadeTotalTime=5f;//淡入淡出的时间public float imageStaticTime=3f;//图片静止时间public float runningTime=10f;//程序运行时间public int StandbyTime=5;//无操作时间/*[HideInInspector]public bool standby=false;*/private bool standby=false;//是否处于待机状态private float startTime=0;//开始待机时间private int imgIndex=2;//图片索引(图片名字)private float remainder=0//下一次待机开始时间private bool hasStartNext=false;//是否已经加载过下一张待机图片private bool canLoad=true;//是否可以加载图片private bool startCountTime=false;//是否可以统计待机时间private int standbyTime=0;//待机时间private int time=0;//帧数,用于统计待机时间private Vector3 prevMousePos=Vector3.zero;//鼠标上一帧所处的位置/*变量说明判断屏幕无操作(而且鼠标的位置要没有变化)后就开始统计无操作的时间(即startCountTime=true,time++(放在FixedUpdate函数中),standbyTime++),当standbyTime超过规定时间,standby=true;开始图片淡入淡出*//*程序刚运行时,不管有没有操作,十秒钟后开始统计开机时间*/IEnumerator StartPrepareStandby(){    yield return new WaitForSeconds(runningTime);    startCountTime=true;//开始统计待机时间}/*进入待机后,显示两张图片,并静止一段时间后在循环淡入淡出*/IEnumerator StartFirstFade(){    //现实两张图片    nobody_Img1.enabled=true;    nobody_Img2.enabled=true;    yield return new WaitForSeconds(imageStaticTime);    //重置时间    startTime=Time.time;//开始待机时间等于程序现在的时间    remainder=startTime;//记录淡入淡出开始的时间    //开始待机    standby=true;}/*第一次淡入淡出后开始以后的淡入淡出循环*/IEnumerator StartNextFade(){    if(imgIndex>=4)//判断图片索引是否越界(图片索引也是图片名)        imgIindex=0;    //canLoad在这用于判断加载哪一张图片    if(canLoad)    {        nobody_Img1.sprite=Resources.Load(imgIndex.ToString(),typeof(Sprite)) as Sprite;    }    else    {        nobody_Img2.sprite=Resources.Load(imgIndex.ToString(),typeof(Sprite)) as Sprite;    }    canLoad = !canLoad;//取反,用于区分图片的加载    imgIndex++;//图片索引累加,下次加载下一张图片    yield return new WaitForSeconds(imageStaticTime);    //重置淡入淡出时间    startTime=Time.time;    remainder=startTime;    //图片已加载,等待下次的加载    hasStartNext=false;}void Start(){    //调用开始统计待机时间的协程    StartCoroutine(StartPrepareStandby());}void FixedUpdate(){    if(startCountTime)    {//无操作下统计时间        if(Input.mousePosition==prevMousePos)        {//判断鼠标是否还在移动            time++;        }        else        {//鼠标移动时,重置待机时间            standbyTime=0;            time=0;        }    }    if(time>=50)    {        time=0;        standbyTime++;//待机秒数    }    if(standbyTime>StandbyTime)    {//超出规定的无操作的时间即认为待机状态        standbyTime--;//开始第一次图片淡入淡出协程只执行一次        startCountTime=false;//停止待机时间的统计        StartCoroutine(StartFirstFade());//开始第一图片淡入淡出    }}void Update(){    if(Input.GetMouseButtonDown(0))    {//每次鼠标按下都停止待机及相关的判断        StopCoroutine(StartNextFade());//停止淡入淡出        standby=false;//退出待机状态        //保留当前图片的alpha值        if(canLoad)        {//根据当前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);        }        //隐藏待机图片         nobody_Img1.enabled=false;         nobody_Img2.enabled=false;         //重置待机时间         standbyTime=0;         time=0;    }    else if(Input.GetMouseButtonUp(0))    {//鼠标每次抬起都认为是无操作        startCountTime=true;        prevMousePos=Input.mousePosition;    }    if(standby)    {        if(Time.time<startTime+fadeTotalTime)        {            float alphaValue=(Time.time-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;//已开始加载下一张照片                StartCoroutine(StartNextFade());//开始下一次淡入淡出协程            }        }    }    prevMousePos=Input.mousePosition;//记录每一帧鼠标的位置}
  1. 总结
    虽然此次感觉思路较为清晰但还是花费了很长时间来完成这一简单的程序;我也深信对这一简单的程序,肯定还有简单的做法;自我感觉自己的程序有点绕且繁琐(对程序的扩展也可以引用设计模式的单例模式或其他模式进行再其他脚本处的待机状态判断)。欢迎各位指点一二!
1 0
原创粉丝点击