Unity 限时使用 限制试用时间和使用次数

【时间限制】修改Start()函数里的minTime和maxTime时间即可。限制时间也可精确到秒,比如:

DateTime minTime = Convert.ToDateTime("2019-4-23 12:22:05");

【次数限制】SetPlayUseNumber()为限制次数方法,修改键值名就可以重新计算("UseTime")

本脚本是限制时间和次数的搭配使用,可自行修改。

 

using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SetUserTime : MonoBehaviour {

    int userNum = 0;
    public Button Btn_expire;

    // Use this for initialization
    void Start () {
        //===(比如3月1日开始计算,到4月1日结束)
        //小于minTime 时间或者大于maxTime时间 ,将不可使用
        DateTime minTime = Convert.ToDateTime("2019-3-1");
        DateTime maxTime = Convert.ToDateTime("2019-4-1");
        if (minTime > DateTime.Now || DateTime.Now > maxTime)
        {
            userNum = 4;
        }
        SetPlayUseNumber();

       
        //if (minTime < DateTime.Now && DateTime.Now < maxTime)
        //{
        //    //===用于限时功能
        //    //在这段时间内能使用xx功能
        //}
    }

    /// <summary>
    /// 设置用户使用次数
    /// </summary>
    void SetPlayUseNumber()
    {
        RegistryKey RootKey, RegKey;
        //项名为:HKEY_CURRENT_USER\Software
        RootKey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true);
        //打开子项:HKEY_CURRENT_USER\Software\MyRegDataApp
        if ((RegKey = RootKey.OpenSubKey("TestToControlUseTime", true)) == null)
        {
            RootKey.CreateSubKey("TestToControlUseTime");       //不存在,则创建子项
            RegKey = RootKey.OpenSubKey("TestToControlUseTime", true);
            RegKey.SetValue("UseTime7", (object)3);              //创建键值,存储可使用次数
            return;
        }
        try
        {
            object usetime = RegKey.GetValue("UseTime7");        //读取键值,可使用次数
            print("还可以使用:" + usetime + "次");
            //int newtime = int.Parse(usetime.ToString()) - 1;
            int newtime = int.Parse(usetime.ToString()) - userNum;
            if (newtime < 0)
            {
                //到期退出程序
                RegKey.SetValue("UseTime7", (object)newtime);
                Btn_expire.gameObject.SetActive(true);
                Invoke("OnExit", 2);//延时退出,可在退出前显示提示消息
            }
            else
            {
                RegKey.SetValue("UseTime7", (object)newtime);    //更新键值,可使用次数减1
            }
        }
        catch
        {
            RegKey.SetValue("UseTime7", (object)3);
            print("更新使用3次");
        }
    }

    private void OnExit()
    {
        Application.Quit();
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章