網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
功能需求:為軟件設(shè)定一個(gè)使用有效期,當(dāng)超過(guò)指定時(shí)間后,程序無(wú)法運(yùn)行。
實(shí)現(xiàn)思路:定義一個(gè)常量,用于記錄一個(gè)時(shí)間,我們稱(chēng)之為標(biāo)記時(shí)間,使用當(dāng)前時(shí)間減去標(biāo)記時(shí)間,如果時(shí)間間隔大于設(shè)定的有效期,退出程序。
具體步驟:
1.定義標(biāo)記時(shí)間常量:
//標(biāo)記時(shí)間 private const string flag = "2022-03-17 17:11:25";
使用DateTime.Parse可將其轉(zhuǎn)換為DateTime類(lèi)型:
DateTime flagTime = DateTime.Parse(flag);
2.獲取當(dāng)前時(shí)間:
DateTime nowTime = DateTime.Now;
3.計(jì)算時(shí)間間隔:
TimeSpan span = nowTime - flagTime;
4.判斷時(shí)間間隔是否大于有效期:
if (span.Days >= expires) Application.Quit();
但是這樣這樣實(shí)現(xiàn)會(huì)有一個(gè)問(wèn)題,DateTime.Now獲取的是本地計(jì)算機(jī)時(shí)間,如果用戶(hù)故意修改計(jì)算機(jī)的時(shí)間,那么這個(gè)功能將無(wú)意義。
因此將獲取當(dāng)前時(shí)間的步驟修改為調(diào)用網(wǎng)絡(luò)接口來(lái)獲取時(shí)間,這里以如下這個(gè)接口為例:
https://apps.game.qq.com/CommArticle/app/reg/gdate.php
使用GET方式調(diào)用接口,代碼如下:
using System; using UnityEngine; using System.Collections; using UnityEngine.Networking; public class Example : MonoBehaviour { //標(biāo)記時(shí)間 private const string flag = "2022-03-17 17:11:25"; //有效期 單位:天 private const int expires = 30; private void Start() { StartCoroutine(RequestCoroutine()); } private IEnumerator RequestCoroutine() string url = "https://apps.game.qq.com/CommArticle/app/reg/gdate.php"; using (UnityWebRequest request = UnityWebRequest.Get(url)) { yield return request.SendWebRequest(); if(request.result == UnityWebRequest.Result.Success) { Debug.Log(request.downloadHandler.text); } else Debug.LogError($"get time failed: {request.error}"); } }
調(diào)用接口我們可以收到如圖所示的響應(yīng),我們只需要通過(guò)Split函數(shù)將字符串分割,獲取到等號(hào)后面的部分,再使用Substring函數(shù)截取‘’符號(hào)中間的部分即可:
string timeStr = request.downloadHandler.text.Split('=')[1]; timeStr = timeStr.Trim().Substring(1, timeStr.Length - 4); Debug.Log(timeStr);
?
?完整代碼:
using System; using UnityEngine; using System.Collections; using UnityEngine.Networking; public class Example : MonoBehaviour { //標(biāo)記時(shí)間 private const string flag = "2022-03-17 17:11:25"; //有效期 單位:天 private const int expires = 30; private void Start() { StartCoroutine(RequestCoroutine()); } private IEnumerator RequestCoroutine() string url = "https://apps.game.qq.com/CommArticle/app/reg/gdate.php"; using (UnityWebRequest request = UnityWebRequest.Get(url)) { yield return request.SendWebRequest(); if(request.result == UnityWebRequest.Result.Success) { Debug.Log(request.downloadHandler.text); string timeStr = request.downloadHandler.text.Split('=')[1]; timeStr = timeStr.Trim().Substring(1, timeStr.Length - 4); Debug.Log(timeStr); DateTime flagTime = DateTime.Parse(flag); DateTime nowTime = DateTime.Parse(timeStr); TimeSpan span = nowTime - flagTime; Debug.Log(span); if (span.Days >= expires) Application.Quit(); } else Debug.LogError($"get time failed: {request.error}"); } }
原文鏈接:https://blog.csdn.net/qq_42139931/article/details/123555866
相關(guān)推薦
- 2022-07-15 Qt各種字符轉(zhuǎn)換的實(shí)現(xiàn)示例_C 語(yǔ)言
- 2022-04-05 Python中hash加密簡(jiǎn)介及使用方法_python
- 2022-05-19 Python?Timer和TimerFPS計(jì)時(shí)工具類(lèi)_python
- 2023-05-29 docker如何對(duì)已經(jīng)啟動(dòng)的容器添加目錄映射(掛載目錄)_docker
- 2022-09-09 C#表達(dá)式和運(yùn)算符詳細(xì)解析_C#教程
- 2022-09-08 Go語(yǔ)言中并發(fā)的工作原理_Golang
- 2022-09-05 SpringBoot 自動(dòng)配置原理
- 2022-04-11 【Android】覺(jué)得不好看?自定義CheckBox樣式
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支