-
大小: 1.19MB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2023-11-20
- 語言: 其他
- 標(biāo)簽:
資源簡介
休閑游戲只有有體力才能進入關(guān)卡,每玩一關(guān)會扣除一個體力,當(dāng)體力不足時不能繼續(xù)游戲,需要購買。本功能是自動恢復(fù)體力,游戲離線也會恢復(fù)體力。

代碼片段和文件信息
using?System.Collections;
using?System.Collections.Generic;
using?UnityEngine;
using?UnityEngine.UI;
using?System;
public?class?EnergyJudge?:?MonoBehaviour
{
????[SerializeField]
????private?Text?energyCountTxt;
????[SerializeField]
????private?Text?timeCountTxt;
????[SerializeField?Header(“surPlusTime轉(zhuǎn)化成時間格式之后的字符串“)]
????private?string?strTime;
????TimeSpan?spanTime;
????[SerializeField?Header(“恢復(fù)下一點體力剩余時間“)]
????private?int?surPlusTime?=?0;//恢復(fù)下一點體力剩余時間
????[SerializeField?Header(“600秒?即10分鐘恢復(fù)一點體力“)]
????public?int?recoverTimeLength?=?600;//600秒?即10分鐘恢復(fù)一點體力
????void?Start()
????{
????????Debug.LogFormat(“進入游戲時間{0}“?DateTime.Now);
????????energyCountTxt.text?=?GetEnergyCount.ToString();
????????spanTime?=?DateTime.Now?-?GetEndGameTime();
????????int?totalLeaveTime?=?(int)Math.Floor(spanTime.TotalSeconds);//游戲離開總時間
????????Debug.LogFormat(“游戲離線時長{0}?秒“?totalLeaveTime);
????????int?countRecover?=?totalLeaveTime?/?recoverTimeLength;//離線了多少個10分鐘
????????SetEnergyCount(GetEnergyCount?+?countRecover);//保存更新之后的體力值
????????if?(GetEnergyCount?>=?20)//如果體力大于20??就把時間時間倒計時設(shè)置為不可見??同時不進行倒計時
????????{
????????????SetGameobjectState(timeCountTxt.gameobject?false);
????????}
????????else??//體力?還是沒有恢復(fù)滿
????????{
????????????int?seconds?=?totalLeaveTime?%?recoverTimeLength;//求出余數(shù)
????????????if?(GetGameOverTime?>?seconds)
????????????{
????????????????surPlusTime?=?GetGameOverTime?-?seconds;
????????????}
????????????else?if?(GetGameOverTime?<=?seconds)//上次退出時??剩余恢復(fù)體力時間,小于離線時間去整之后的余數(shù),說明體力還可以加一點
????????????{
????????????????surPlusTime?=?recoverTimeLength?-?(seconds?-?GetGameOverTime);
????????????????SetEnergyCount(GetEnergyCount?+?1);//保存更新之后的體力值??體力加1
????????????????if?(GetEnergyCount?>=?20)
????????????????{
????????????????????SetGameobjectState(timeCountTxt.gameobject?false);
????????????????}
????????????}
????????????Debug.LogFormat(“恢復(fù)下一點體力剩余時間{0}“?surPlusTime);
????????}
????????FormatLongToTimeStr(surPlusTime);
????????timeCountTxt.text?=?strTime;
????????energyCountTxt.text?=?GetEnergyCount.ToString();
????}
????void?Update()
????{
????????//測試體力增加減少
????????TestReduceEnergy();
????????CalcTimer();
????}
????#region?計時器
????public?float?timer?=?0;
????///?
????///?體力值是否滿了??20為滿
????///?
????public?bool?IsFullEnergy
????{
????????get
????????{
????????????if?(GetEnergyCount?20)
????????????{
????????????????return?false;
????????????}
????????????return?true;
????????}
????}
????void?CalcTimer()
????{
????????if?(IsFullEnergy)//如果體力是滿的,就讓恢復(fù)體力的剩余時間為0
????????{
????????????timer?=?0;
????????????surPlusTime?=?recoverTimeLength;
????????????timeCountTxt.text?=?strTime;//把轉(zhuǎn)化成時間格式的字符串更新到UI
????????????SetGameobjectState(timeCountTxt.gameobject?false);
????????????return;
????????}
????????else
????????{
????????????SetGameobjectState(timeCountTxt.gameobject?true);
????????}
????????time
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????138104??2019-12-16?20:54??EnergyJudge\1576500899(1).jpg
?????文件?????130899??2019-12-16?20:55??EnergyJudge\1576500956(1).jpg
?????文件???????7612??2019-12-16?20:30??EnergyJudge\EnergyJudge.cs
?????文件????1067020??2019-12-16?20:42??EnergyJudge\GIF.gif
?????目錄??????????0??2019-12-16?20:56??EnergyJudge
-----------?---------??----------?-----??----
??????????????1343635????????????????????5
評論
共有 條評論