Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plug-in sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

One, foreword

When using Unity for software development, will encounter the implementation of the number of times of use, as well as the use of the time limit, is a measure to protect the program. The method we use is to create a new registry, increase the key value pair, modify the key value, complete the use of the program permission control. Of course, there are more secure methods, including obtaining hard drive numbers, and electronic dog encryption, etc., which will be discussed later.

Second, time limit

Change minTime and maxTime in the Start() function. The time limit can also be accurate to the second, such as:

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

Code:

using System;
using UnityEngine;

public class SetUserTime : MonoBehaviour
{
    // Whether the user has expired
    bool Islate = false;

    // Use this for initialization
    void Start()
    {
        //=== (for example, start on August 1 and end on August 8)
        // If the value is smaller than minTime or greater than maxTime, it is unavailable
        DateTime minTime = Convert.ToDateTime("The 2019-8-1 15:29:00");
        DateTime maxTime = Convert.ToDateTime("The 2019-8-8 15:29:00");
        if (minTime > DateTime.Now || DateTime.Now > maxTime)
        {
            // Exit the program when it is not in use
            Islate = true;
        }
        SetPlayUseNumber();
    }

    /// <summary>
    ///Set user usage times
    /// </summary>
    void SetPlayUseNumber()
    {
        // Exception catch. If an exception occurs, such as flash, the limit is changed to false
        try
        {
            // Limit the use time, if not within this interval, directly exit the program
            if (Islate)
            {
                Invoke("OnExit".2);// Delay exit, can be displayed before exit prompt message
            }
        }
        catch
        {
            Islate = false; }}// Source procedure
    private void OnExit(){ Application.Quit(); }}Copy the code

This is where the time is used to determine what this program can use

Start time: DateTime minTime = convert. ToDateTime(“2019-8-1 15:29:00”); End time: DateTime maxTime = convert. ToDateTime(“2019-8-8 15:29:00”);

This script is mounted to the program startup page, and each time the program starts, the program first determines whether the program is valid.

The program will automatically exit if it is not valid.

3. Frequency limit

SetPlayUseNumber() is a limited-count method that can be recalculated by changing the key name (“UseTime”).

This script is limited in time and times of collocation, can be modified.

Script:

using Microsoft.Win32;
using UnityEngine;

public class SetUserTime1 : MonoBehaviour
{
    // Maximum usage
    int MaxUsageCount = 3;

    void Start()
    {
        SetPlayUseNumber();
    }

    /// <summary>
    ///Set user usage times
    /// </summary>
    void SetPlayUseNumber()
    {
        // Create a key-value pair
        RegistryKey RootKey, RegKey;
        // The name of the item is HKEY_CURRENT_USER\Software
        RootKey = Registry.CurrentUser.OpenSubKey("SOFTWARE".true);
        // Open the subitem: HKEY_CURRENT_USER\Software\MyRegDataApp
        if ((RegKey = RootKey.OpenSubKey("TestToControlUseTime".true)) = =null)
        {
            RootKey.CreateSubKey("TestToControlUseTime");               // If no, create a subitem
            RegKey = RootKey.OpenSubKey("TestToControlUseTime".true);  // Open the key
            RegKey.SetValue("UseTime7", (object)MaxUsageCount);         // Create a key value to store the maximum number of times it can be used
            return;
        }
        If a program exception occurs, such as a flash exit, the number of times is updated to the maximum number of times set at the start
        try
        {
            object usetime = RegKey.GetValue("UseTime7");        // Read the key value, the number of times it can be used
            print("Can also use :" + usetime + "Time");
            // Reduce the number of uses by 1
            int newtime = int.Parse(usetime.ToString()) - 1;
            if (newtime < 0)
            {
                // Exit the program at expiration
                RegKey.SetValue("UseTime7", (object)newtime);
                Invoke("OnExit".2);// Delay exit, can be displayed before exit prompt message
            }
            else
            {
                RegKey.SetValue("UseTime7", (object)newtime);    // Update the key value, reduce the number of uses by 1
            }
        }
        catch
        {
            RegKey.SetValue("UseTime7", (object)MaxUsageCount);
            print("Update usage"); }}/// <summary>
    ///Exit the program
    /// </summary>
    private void OnExit(){ Application.Quit(); }}Copy the code

This method mainly uses the registry. Every Win system has a registry in which data can be written

Each time the program is started, it first determines whether there is a value in the registry that is greater than zero

If it’s less than zero, the program exits automatically.

The downside of this approach, however, is that if the program code is cracked, you can find the registry and modify it.

Four, at the same time control time and times

using Microsoft.Win32;
using System;
using UnityEngine;

public class SetUserTime2 : MonoBehaviour
{
    // Maximum usage
    int MaxUsageCount = 3;
    // Whether the user has expired
    bool Islate = false;

    void Start()
    {
        //=== (for example, start on August 1 and end on August 8)
        // If the value is smaller than minTime or greater than maxTime, it is unavailable
        DateTime minTime = Convert.ToDateTime("The 2019-8-1 15:29:00");
        DateTime maxTime = Convert.ToDateTime("The 2019-8-8 15:29:00");
        if (minTime > DateTime.Now || DateTime.Now > maxTime)
        {
            // Exit the program when it is not in use
            Islate = true;
        }
        SetPlayUseNumber();
    }

    /// <summary>
    ///Set user usage times
    /// </summary>
    void SetPlayUseNumber()
    {
        // Create a key-value pair
        RegistryKey RootKey, RegKey;
        // The name of the item is HKEY_CURRENT_USER\Software
        RootKey = Registry.CurrentUser.OpenSubKey("SOFTWARE".true);
        // Open the subitem: HKEY_CURRENT_USER\Software\MyRegDataApp
        if ((RegKey = RootKey.OpenSubKey("TestToControlUseTime".true)) = =null)
        {
            RootKey.CreateSubKey("TestToControlUseTime");               // If no, create a subitem
            RegKey = RootKey.OpenSubKey("TestToControlUseTime".true);  // Open the key
            RegKey.SetValue("UseTime7", (object)MaxUsageCount);         // Create a key value to store the maximum number of times it can be used
            return;
        }
        If a program exception occurs, such as a flash exit, the number of times is updated to the maximum number of times set at the start
        try
        {
            object usetime = RegKey.GetValue("UseTime7");        // Read the key value, the number of times it can be used
            print("Can also use :" + usetime + "Time");
            // Reduce the number of uses by 1
            int newtime = int.Parse(usetime.ToString()) - 1;
            if (newtime < 0 || Islate)
            {
                // Exit the program at expiration
                RegKey.SetValue("UseTime7", (object)newtime);
                Invoke("OnExit".2);// Delay exit, can be displayed before exit prompt message
            }
            else
            {
                RegKey.SetValue("UseTime7", (object)newtime);    // Update the key value, reduce the number of uses by 1
            }
        }
        catch
        {
            RegKey.SetValue("UseTime7", (object)MaxUsageCount);
            Islate = false;
            print("Update usage"); }}/// <summary>
    ///Exit the program
    /// </summary>
    private void OnExit(){ Application.Quit(); }}Copy the code

This approach takes the advantage of time and frequency and improves the disadvantage, even if you modify the registry.

There will be time to judge.

However, it can also be cracked while modifying the local computer.

The best way to do it is to take the hard drive code of the computer and generate a unique identifier and send it to the server, and the server will then set the start time and the end time, so that every time you go into the program, it will determine the time first.