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);    //更新
            }
        }
        catch
        {
            RegKey.SetValue("UseTime7", (object)3);
            print("更新使用3次");
        }
    }

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

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值