今天小编遇到这样一个需求需要为软件设定一个使用有效期,当超过指定时间后,程序无法执行,实现思路并不复杂,今天小编通过本文给大家分享Unity为软件添加使用有效期的具体步骤,感兴趣的朋友一起看看吧
功能需求:为软件设定一个使用有效期,当超过指定时间后,程序无法运行。
实现思路:定义一个常量,用于记录一个时间,我们称之为标记时间,使用当前时间减去标记时间,如果时间间隔大于设定的有效期,退出程序。
具体步骤:
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}");
}
}
到此这篇关于Unity为软件添加使用有效期的文章就介绍到这了,更多相关Unity软件使用有效期内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
本文标题为:Unity为软件添加使用有效期的具体步骤


基础教程推荐
- C#中的Linq to JSON操作详解 2023-06-08
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- Unity shader实现高斯模糊效果 2023-01-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C# 解析XML和反序列化的示例 2023-04-14