zoukankan      html  css  js  c++  java
  • Unity 限时使用 限制试用时间和使用次数

    //***************************************************
    //作用:限制使用时间或次数
    //用法:时间限制,修改Start()函数里的方法即可
    //SetPlayUseNumber()为限制次数方法,修改键值名就可以重新计算("UseTime")
    //
    //
    //***************************************************
    using Microsoft.Win32;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class SetUserTime : MonoBehaviour {
    
        //注册名
        string UseTime = "UseTime15";
        //试用次数
        int num = 1100;
        //到期提示文字
        public Button Btn_expire;
    
        // Use this for initialization
        void Start () {
            //===(比如从3月1日开始计算,到4月1日结束)
            //只要小于minTime时间或大于maxTime时间,将停止使用
            DateTime minTime = Convert.ToDateTime("2020-7-1");
            DateTime maxTime = Convert.ToDateTime("2020-8-1");
            if (minTime > DateTime.Now || DateTime.Now > maxTime)
            {
                //===到期后,超时提示
                UseTime = "到期了";
                num = 0;
                print("到期了");
            }
    
            //===本方法放在此处,可以实现时间和次数同时限制
            SetPlayUseNumber();
    
    
            //if (minTime < DateTime.Now && DateTime.Now < maxTime)
            //{
            //    //===用于限时功能
            //    //在这段时间内能使用xx功能
            //}
        }
    
        /// <summary>
        /// 设置用户使用次数
        /// </summary>
        void SetPlayUseNumber()
        {
            RegistryKey RootKey, RegKey;
            //项名为:HKEY_CURRENT_USERSoftware
            RootKey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true);
            //打开子项:HKEY_CURRENT_USERSoftwareMyRegDataApp
            if ((RegKey = RootKey.OpenSubKey("ScaffoldProjectToControlUseTime", true)) == null)
            {
                RootKey.CreateSubKey("ScaffoldProjectToControlUseTime");       //不存在,则创建子项
                RegKey = RootKey.OpenSubKey("ScaffoldProjectToControlUseTime", true);
                RegKey.SetValue(UseTime, (object)num);              //创建键值,存储可使用次数
                print("实例化");
                return;
            }
            try
            {
                object usetime = RegKey.GetValue(UseTime);        //读取键值,可使用次数
                print("还可以使用:" + usetime + "");
                int newtime = int.Parse(usetime.ToString()) - 1;//可使用次数减1
                if (newtime < 0)
                {
                    //到期退出程序
                    RegKey.SetValue(UseTime, (object)newtime);//20190225添加
                    Btn_expire.gameObject.SetActive(true);
                    //Btn_expire.gameObject.transform.GetChild(0).GetComponent<Text>().text="试用已到期,请联系管理员";
    
                    Invoke("OnQuit", 3);//延时退出
                }
                else
                {
                    RegKey.SetValue(UseTime, (object)newtime);    //更新键值,可使用次数减1
                    //Btn_expire.gameObject.SetActive(false);
    
                }
            }
            catch
            {
                RegKey.SetValue(UseTime, (object)num);
                print("首次进入,更新使用次数");
            }
        }
    
        private void OnQuit()
        {
            Application.Quit();
        }
    }

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

    DateTime minTime = Convert.ToDateTime("2019-4-23 12:22:05");
    【次数限制】SetPlayUseNumber()为限制次数方法,修改键值名就可以重新计算("UseTime")

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

    注:每次到期后(次数和时间),UseTime的值要变

  • 相关阅读:
    Python 日志处理(三) 日志状态码分析、浏览器分析
    Python 日志处理(二) 使用正则表达式处理Nginx 日志
    mongodb关联查询 和spring data mongodb
    redis实现分布式锁
    springboot使用过滤器和拦截器
    springboot使用schedule定时任务
    fastjson格式化输出内容
    logback-spring.xml
    spring data jpa封装specification实现简单风格的动态查询
    spring data jpa自定义baseRepository
  • 原文地址:https://www.cnblogs.com/WalkingSnail/p/13219113.html
Copyright © 2011-2022 走看看