[Unity3D] to achieve "usage restrictions and the time limit function"

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/q764424567/article/details/98076243

I. Introduction

When using Unity software development, the program will encounter the number of times that implement the restrictions, and restrictions on the use of the time interval is a measure to protect the program, said White. The method we use, the new registry key values ​​to increase, change key, complete authority to use the program's control. Of course, there are safer ways, including access to the hard disk number, and encrypted electronic dog, etc., which will discuss later.

Second, reference articles

Unity restrictions limit the use of trial time and frequency of use
https://blog.csdn.net/zjh_368/article/details/87920383
Author: Ziyang _368

Third, the time limit

Modify Start function in the () minTime and maxTime time can be. Time limit may be accurate to the second, for example:
the DateTime = minTime to Convert.ToDateTime ( "2019-4-23 12:22:05");

Code:

using System;
using UnityEngine;

public class SetUserTime : MonoBehaviour
{
    //用户是否超过使用日期
    bool Islate = false;

    // Use this for initialization
    void Start()
    {
        //===(比如8月1日开始计算,到8月8日结束)
        //小于minTime 时间或者大于maxTime时间 ,将不可使用
        DateTime minTime = Convert.ToDateTime("2019-8-1 15:29:00");
        DateTime maxTime = Convert.ToDateTime("2019-8-8 15:29:00");
        if (minTime > DateTime.Now || DateTime.Now > maxTime)
        {
            //不在使用时间内,会直接退出程序
            Islate = true;
        }
        SetPlayUseNumber();
    }

    /// <summary>
    /// 设置用户使用次数
    /// </summary>
    void SetPlayUseNumber()
    {
        //异常捕捉,如果发生异常,比如闪退,限制改为false
        try
        {
            //限制使用时间,如果不在这个区间内,直接退出程序
            if (Islate)
            {
                Invoke("OnExit", 2);//延时退出,可在退出前显示提示消息
            }
        }
        catch
        {
            Islate = false;
        }
    }

    //出处程序
    private void OnExit()
    {
        Application.Quit();
    }
}

Fourth, limit the number of

SetPlayUseNumber () method to limit the number of times, modify the key names can be recalculated ( "UseTime")

This script is to limit the time and frequency of use with, can modify.

script:

using Microsoft.Win32;
using UnityEngine;

public class SetUserTime1 : MonoBehaviour
{
    //最大使用次数
    int MaxUsageCount = 3;

    void Start()
    {
        SetPlayUseNumber();
    }

    /// <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)MaxUsageCount);         //创建键值,存储最大可使用次数
            return;
        }
        //异常捕捉,如果出现程序异常,比如闪退,次数更新为开始设置的最大使用次数
        try
        {
            object usetime = RegKey.GetValue("UseTime7");        //读取键值,可使用次数
            print("还可以使用:" + usetime + "次");
            //使用次数减1
            int newtime = int.Parse(usetime.ToString()) - 1;
            if (newtime < 0)
            {
                //到期退出程序
                RegKey.SetValue("UseTime7", (object)newtime);
                Invoke("OnExit", 2);//延时退出,可在退出前显示提示消息
            }
            else
            {
                RegKey.SetValue("UseTime7", (object)newtime);    //更新键值,可使用次数减1
            }
        }
        catch
        {
            RegKey.SetValue("UseTime7", (object)MaxUsageCount);
            print("更新使用次数");
        }
    }

    /// <summary>
    /// 退出程序
    /// </summary>
    private void OnExit()
    {
        Application.Quit();
    }
}

Fifth, while controlling the time and frequency

using Microsoft.Win32;
using System;
using UnityEngine;

public class SetUserTime2 : MonoBehaviour
{
    //最大使用次数
    int MaxUsageCount = 3;
    //用户是否超过使用日期
    bool Islate = false;

    void Start()
    {
        //===(比如8月1日开始计算,到8月8日结束)
        //小于minTime 时间或者大于maxTime时间 ,将不可使用
        DateTime minTime = Convert.ToDateTime("2019-8-1 15:29:00");
        DateTime maxTime = Convert.ToDateTime("2019-8-8 15:29:00");
        if (minTime > DateTime.Now || DateTime.Now > maxTime)
        {
            //不在使用时间内,会直接退出程序
            Islate = true;
        }
        SetPlayUseNumber();
    }

    /// <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)MaxUsageCount);         //创建键值,存储最大可使用次数
            return;
        }
        //异常捕捉,如果出现程序异常,比如闪退,次数更新为开始设置的最大使用次数
        try
        {
            object usetime = RegKey.GetValue("UseTime7");        //读取键值,可使用次数
            print("还可以使用:" + usetime + "次");
            //使用次数减1
            int newtime = int.Parse(usetime.ToString()) - 1;
            if (newtime < 0 || Islate)
            {
                //到期退出程序
                RegKey.SetValue("UseTime7", (object)newtime);
                Invoke("OnExit", 2);//延时退出,可在退出前显示提示消息
            }
            else
            {
                RegKey.SetValue("UseTime7", (object)newtime);    //更新键值,可使用次数减1
            }
        }
        catch
        {
            RegKey.SetValue("UseTime7", (object)MaxUsageCount);
            Islate = false;
            print("更新使用次数");
        }
    }

    /// <summary>
    /// 退出程序
    /// </summary>
    private void OnExit()
    {
        Application.Quit();
    }
}

Guess you like

Origin blog.csdn.net/q764424567/article/details/98076243
Recommended