This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

An overview of the

In the development of the project to others for experience, sometimes afraid of the program is white piao, encryption equipment and do not need, a bit of a fuss, so there is a time encryption program said. This function is to add a display to the program so that it can not be used indefinitely, but can only be experienced for a specified period of time. This is why we need to add a time limit to the program, for example, the software cannot be used after YYYY, MM, dd. However, there is a drawback to this method, which is that some people, such as developers, may easily ignore this encryption.

Analysis of the

Time encryption, in fact, is to obtain a local time/network time, and then compared with the time we set, if we set the time after the time obtained, then the software can experience, otherwise the program automatically exit or black screen (can be edited by the user)

The advantages and disadvantages

Advantages:

  • Can play a protective role to the program, prevent the fruits of labor to be stolen

Disadvantages:

  • With network time encryption, the device must be accessed online
  • With local time encryption, as long as the system time is changed before the set time, then the program can be used without limit.

Function implementation

Time to get

The first step is to obtain the time, normally speaking, the use of encryption time is generally in the start of the program to obtain a time, if you want to real-time verification can also, just need to write a coroutine, set a cycle to obtain the time.

Get local time

Getting local time in Unity is as simple as a single sentence. The prerequisite is a namespace: using UnityEngine;

System.DateTime date1 = System.DateTime.Now; // System time int nowYear = date.year; // year int nowMouth = date.month; // month int nowDay = date.day; / /,Copy the code

Get network time

Obtaining network time, as the name implies, means that the program needs to be connected to the Internet to obtain time. If the device is not connected to the Internet and fails to obtain time, it will directly exit. WebRequest is used here to obtain the network time code as follows: return time string on success, null on failure. We mainly validate dates, so as long as dates don’t have time, use ToString(” YYYY-MM-DD “) to format the time

public string GetNetDateTime() { WebRequest request = null; WebResponse response = null; WebHeaderCollection headerCollection = null; string datetime = string.Empty; DateTime timeNow = DateTime.MinValue; try { request = WebRequest.Create("https://www.baidu.com"); request.Timeout = 3000; request.Credentials = CredentialCache.DefaultCredentials; response = (WebResponse)request.GetResponse(); headerCollection = response.Headers; foreach (var h in headerCollection.AllKeys) { if (h == "Date") { datetime = headerCollection[h]; timeNow = Convert.ToDateTime(datetime); } } return timeNow.ToString("yyyy-MM-dd"); } catch (Exception) {debug.log (" get network time error "); return null; } finally { if (request ! = null) { request.Abort(); } if (response ! = null) { response.Close(); } if (headerCollection ! = null) { headerCollection.Clear(); }}}Copy the code

Time to verify

The time has been obtained, so the next step is to compare with the set time. For example: 2021-08-27 time interception, Split(‘-‘) is used to cut it into a string array, and then convert it to an int type for comparison

The Convert. ToInt32 (Date. The Split (' - ') [0]) / / in the Convert. ToInt32 (Date. The Split (' - ') [1]). / / the Convert ToInt32 (Date. The Split (' - ') [2]) / / dayCopy the code

Judging order is, phase judge year, then judge month, finally judge date, concrete implementation logic is as follows

If (year < convert.toint32 (date.split ('-')[0])) {// Verify by executing method here} else if (year == Convert.toint32 (date.split ('-')[0])) {if (mouth < convert.toint32 (date.split ('-')[1])) {// Validate by executing the method here} else if (mouth = = the Convert. ToInt32 (Date. The Split (' - ') [1])) {if (day < = the Convert. ToInt32 (Date. The Split (' - ') [2])) {/ / verification through execution method here} else {# if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif } } else { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif } } else { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit(); #endif }Copy the code

Implementation effect

Source code sharing

GitHub download address: Click here to skip to download

The source code has an enumeration for the choice of authentication methods, and a public string for setting the encryption date

Write in the last

All the shared content is the author used in the daily development process of a variety of small function points, sharing is also a way to review, if there is a bad place to write, please give more advice. Welcome to learn from each other and make progress. This piece article is written here first, hope to be able to help you