前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity【DateTime】- 如何为软件添加使用有效期

Unity【DateTime】- 如何为软件添加使用有效期

作者头像
CoderZ
发布2022-08-29 16:50:19
1.4K0
发布2022-08-29 16:50:19
举报
文章被收录于专栏:用户10004205的专栏

功能需求:为软件设定一个使用有效期,当超过指定时间后,程序无法运行。

实现思路:定义一个常量,用于记录一个时间,我们称之为标记时间,使用当前时间减去标记时间,如果时间间隔大于设定的有效期,退出程序。

具体步骤:

1.定义标记时间常量:

代码语言:javascript
复制
//标记时间
private const string flag = "2022-03-17 17:11:25";

使用DateTime.Parse可将其转换为DateTime类型:

代码语言:javascript
复制
DateTime flagTime = DateTime.Parse(flag);

2.获取当前时间:

代码语言:javascript
复制
DateTime nowTime = DateTime.Now;

3.计算时间间隔:

代码语言:javascript
复制
TimeSpan span = nowTime - flagTime;

4.判断时间间隔是否大于有效期:

代码语言:javascript
复制
if (span.Days >= expires) Application.Quit();

但是这样实现会有一个问题,DateTime.Now获取的是本地计算机时间,如果用户故意修改计算机的时间,那么这个功能将无意义。

因此将获取当前时间的步骤修改为调用网络接口来获取时间,这里以如下这个接口为例:

https://apps.game.qq.com/CommArticle/app/reg/gdate.php

使用GET方式调用接口,代码如下:

代码语言:javascript
复制
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函数截取‘’符号中间的部分即可:

代码语言:javascript
复制
string timeStr = request.downloadHandler.text.Split('=')[1];
timeStr = timeStr.Trim().Substring(1, timeStr.Length - 4);
Debug.Log(timeStr);

完整代码:

代码语言:javascript
复制
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}");
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 当代野生程序猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档