Unity為軟件添加使用有效期的具體步驟
功能需求:為軟件設(shè)定一個(gè)使用有效期,當(dāng)超過指定時(shí)間后,程序無法運(yùn)行。
實(shí)現(xiàn)思路:定義一個(gè)常量,用于記錄一個(gè)時(shí)間,我們稱之為標(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類型:
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è)問題,DateTime.Now獲取的是本地計(jì)算機(jī)時(shí)間,如果用戶故意修改計(jì)算機(jī)的時(shí)間,那么這個(gè)功能將無意義。
因此將獲取當(dāng)前時(shí)間的步驟修改為調(diào)用網(wǎng)絡(luò)接口來獲取時(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),我們只需要通過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}"); } }
到此這篇關(guān)于Unity為軟件添加使用有效期的文章就介紹到這了,更多相關(guān)Unity軟件使用有效期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法
這篇文章主要介紹了C#區(qū)分中英文按照指定長(zhǎng)度截取字符串的方法,涉及C#操作字符串的正則匹配與截取等常用操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03Unity實(shí)戰(zhàn)之FlyPin(見縫插針)小游戲的實(shí)現(xiàn)
這篇文章主要介紹了利用Unity制作FlyPin(見縫插針)小游戲的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試2022-01-01詳解C# 利用反射根據(jù)類名創(chuàng)建類的實(shí)例對(duì)象
這篇文章主要介紹了詳解C# 利用反射根據(jù)類名創(chuàng)建類的實(shí)例對(duì)象,“反射”其實(shí)就是利用程序集的元數(shù)據(jù)信息,感興趣的小伙伴們可以參考一下。2017-03-03C#實(shí)現(xiàn)接口base調(diào)用示例詳解
這篇文章主要為大家介紹了C#實(shí)現(xiàn)接口base調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06