網站首頁 編程語言 正文
功能需求:為軟件設定一個使用有效期,當超過指定時間后,程序無法運行。
實現思路:定義一個常量,用于記錄一個時間,我們稱之為標記時間,使用當前時間減去標記時間,如果時間間隔大于設定的有效期,退出程序。
具體步驟:
1.定義標記時間常量:
//標記時間 private const string flag = "2022-03-17 17:11:25";
使用DateTime.Parse可將其轉換為DateTime類型:
DateTime flagTime = DateTime.Parse(flag);
2.獲取當前時間:
DateTime nowTime = DateTime.Now;
3.計算時間間隔:
TimeSpan span = nowTime - flagTime;
4.判斷時間間隔是否大于有效期:
if (span.Days >= expires) Application.Quit();
但是這樣這樣實現會有一個問題,DateTime.Now獲取的是本地計算機時間,如果用戶故意修改計算機的時間,那么這個功能將無意義。
因此將獲取當前時間的步驟修改為調用網絡接口來獲取時間,這里以如下這個接口為例:
https://apps.game.qq.com/CommArticle/app/reg/gdate.php
使用GET方式調用接口,代碼如下:
using System; using UnityEngine; using System.Collections; using UnityEngine.Networking; public class Example : MonoBehaviour { //標記時間 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}"); } }
調用接口我們可以收到如圖所示的響應,我們只需要通過Split函數將字符串分割,獲取到等號后面的部分,再使用Substring函數截取‘’符號中間的部分即可:
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 { //標記時間 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
相關推薦
- 2022-04-05 本地文件提交到gitee上報錯的問題:error: src refspec master does
- 2022-04-27 python技巧分享Excel創建和修改_python
- 2022-06-08 基于Apache?Hudi在Google云構建數據湖平臺的思路詳解_Linux
- 2022-02-17 Error: Network Error
- 2022-08-19 Exception evaluating SpringEL expression異常處理
- 2024-03-17 WSL子系統啟動報錯 Wsl/Service/CreateInstance/CreateVm/HCS
- 2022-10-19 python類參數定義及數據擴展方式unsqueeze/expand_python
- 2022-03-27 Android?Studio實現井字游戲_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支