Csharp/C#教程:Unity3D实现待机状态图片循环淡入淡出分享

本文实例为大家分享了Unity3D实现图片循环淡入淡出的具体代码,供大家参考,具体内容如下

1、说明

由于近期项目需求,在网上找了淡入淡出的代码,但都是两张图片淡入淡出结束后就没有了(可能我没找到其他好的作品)。所以我做了简单的扩展

2、简单思路

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

3、代码实现及分析

引入UI命名空间,用UI做淡入淡出效果;

usingUnityEngine.UI; publicImagenobody_Img1; publicImagenobody_Img2;//淡入淡出的两张图片 publicfloatfadeTotalTime=5f;//淡入淡出的时间 publicfloatimageStaticTime=3f;//图片静止时间 publicfloatrunningTime=10f;//程序运行时间 publicintStandbyTime=5;//无操作时间 /* [HideInInspector] publicboolstandby=false; */ privateboolstandby=false;//是否处于待机状态 privatefloatstartTime=0;//开始待机时间 privateintimgIndex=2;//图片索引(图片名字) privatefloatremainder=0//下一次待机开始时间 privateboolhasStartNext=false;//是否已经加载过下一张待机图片 privateboolcanLoad=true;//是否可以加载图片 privateboolstartCountTime=false;//是否可以统计待机时间 privateintstandbyTime=0;//待机时间 privateinttime=0;//帧数,用于统计待机时间 privateVector3prevMousePos=Vector3.zero;//鼠标上一帧所处的位置 /*变量说明 判断屏幕无操作(而且鼠标的位置要没有变化)后就开始统计无操作的时间(即startCountTime=true,time++(放在FixedUpdate函数中),standbyTime++),当standbyTime超过规定时间,standby=true;开始图片淡入淡出 */ /* 程序刚运行时,不管有没有操作,十秒钟后开始统计开机时间 */ IEnumeratorStartPrepareStandby() { yieldreturnnewWaitForSeconds(runningTime); startCountTime=true;//开始统计待机时间 } /* 进入待机后,显示两张图片,并静止一段时间后在循环淡入淡出 */ IEnumeratorStartFirstFade() { //现实两张图片 nobody_Img1.enabled=true; nobody_Img2.enabled=true; yieldreturnnewWaitForSeconds(imageStaticTime); //重置时间 startTime=Time.time;//开始待机时间等于程序现在的时间 remainder=startTime;//记录淡入淡出开始的时间 //开始待机 standby=true; } /* 第一次淡入淡出后开始以后的淡入淡出循环 */ IEnumeratorStartNextFade() { if(imgIndex>=4)//判断图片索引是否越界(图片索引也是图片名) imgIindex=0; //canLoad在这用于判断加载哪一张图片 if(canLoad) { nobody_Img1.sprite=Resources.Load(imgIndex.ToString(),typeof(Sprite))asSprite; } else { nobody_Img2.sprite=Resources.Load(imgIndex.ToString(),typeof(Sprite))asSprite; } canLoad=!canLoad;//取反,用于区分图片的加载 imgIndex++;//图片索引累加,下次加载下一张图片 yieldreturnnewWaitForSeconds(imageStaticTime); //重置淡入淡出时间 startTime=Time.time; remainder=startTime; //图片已加载,等待下次的加载 hasStartNext=false; } voidStart() { //调用开始统计待机时间的协程 StartCoroutine(StartPrepareStandby()); } voidFixedUpdate() { 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());//开始第一图片淡入淡出 } } voidUpdate() { if(Input.GetMouseButtonDown(0)) {//每次鼠标按下都停止待机及相关的判断 StopCoroutine(StartNextFade());//停止淡入淡出 standby=false;//退出待机状态 //保留当前图片的alpha值 if(canLoad) {//根据当前canLoad来判断那一张照片在淡入淡出 nobody_Img1.color=newColor(1,1,1,1); nobody_Img2.color=newColor(1,1,1,0); } else { nobody_Img1.color=newColor(1,1,1,0); nobody_Img2.color=newColor(1,1,1,1); } //隐藏待机图片 nobody_Img1.enabled=false; nobody_Img2.enabled=false; //重置待机时间 standbyTime=0; time=0; } elseif(Input.GetMouseButtonUp(0)) {//鼠标每次抬起都认为是无操作 startCountTime=true; prevMousePos=Input.mousePosition; } if(standby) { if(Time.time<startTime+fadeTotalTime) { floatalphaValue=(Time.time-remainder)/fadeTotalTime; if(canLoad) { nobody_Img1.color=newColor(1,1,1,1-alphaValue); nobody_Img2.color=newColor(1,1,1,alphaValue); } else { nobody_Img1.color=newColor(1,1,1,alphaValue); nobody_Img2.color=newColor(1,1,1,1-alphaValue); } } else { if(!hasStartNext) { hasStartNext=true;//已开始加载下一张照片 StartCoroutine(StartNextFade());//开始下一次淡入淡出协程 } } } prevMousePos=Input.mousePosition;//记录每一帧鼠标的位置 }

上述就是C#学习教程:Unity3D实现待机状态图片循环淡入淡出分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ctvol.com/cdevelopment/903949.html

(0)
上一篇 2021年10月21日
下一篇 2021年10月21日

精彩推荐