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

  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. 总结
    虽然此次感觉思路较为清晰但还是花费了很长时间来完成这一简单的程序;我也深信对这一简单的程序,肯定还有简单的做法;自我感觉自己的程序有点绕且繁琐(对程序的扩展也可以引用设计模式的单例模式或其他模式进行再其他脚本处的待机状态判断)。欢迎各位指点一二!
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛·枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值