Unity 软件伪授权码

本文介绍了一种在Unity中实现的软件授权码验证机制。通过输入授权码,该机制能够控制软件的运行状态,未输入正确授权码将限制软件功能。代码使用C#编写,涉及时间计算、GUI界面设计及PlayerPrefs数据存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

输入授权码后软件可正常运行,否则各种错误

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class XKSecurity : MonoBehaviour {
	
	string _key = "这里输入你的授权码";

	string _history = "2019-11-1";

	int _openDay = 2;

	bool _isOpen = true;


	int _winWidth;
	int _winHeight;

	void Start () {

		_winWidth = Screen.width;
		_winHeight = Screen.height;
		_itemHeight = _winHeight / 10;
		_startX = _winWidth / 5;
		int openDay = _openDay;
		DateTime historyTime = Convert.ToDateTime(_history);
		System.DateTime currentTime = System.DateTime.Now;
		TimeSpan ts = currentTime - historyTime;
		//PlayerPrefs.SetInt("XKSecurity", 1);
		_openDay = PlayerPrefs.GetInt("XKSecurity", _openDay);
		if(ts.Days>_openDay)
		{
			_isOpen = false;
		}else
		{
			Destroy(gameObject);
		}
	}	
	string key;
	int _InternalHeight = 20;
	int _itemHeight;
	int _itemWidth;
	int _startX;

	bool _isOpenSuccess = false;
	bool _isOpenfail = false;
	private void OnGUI()
	{
		if (!_isOpen)
		{
			//去掉材质
			MeshRenderer[] gos = GameObject.FindObjectsOfType<MeshRenderer>();
			foreach(var go in gos)
			{
				 Destroy(go);
			}

			//去掉ui事件
			UnityEngine.EventSystems.EventSystem uievent = GameObject.FindObjectOfType<UnityEngine.EventSystems.EventSystem>();
			if(uievent)
			{
				Destroy(uievent.gameObject);
			}
			//去掉uiCanvas
			UnityEngine.UI.CanvasScaler canvasScaler = GameObject.FindObjectOfType<UnityEngine.UI.CanvasScaler>();
			if (canvasScaler)
			{
				Destroy(canvasScaler.gameObject);
			}

			key = GUI.TextField(new Rect(_startX, _winHeight/5, _winHeight/2, _itemHeight), key);
			GUIStyle fontStyle = new GUIStyle();         
			fontStyle.fontSize = 40;
								 
			GUI.Label(new Rect(_startX, _winHeight / 5 + _itemHeight+ _InternalHeight, _winWidth/ 2, _itemHeight), "请输入轩科授权码", fontStyle);
			GUI.Label(new Rect(_startX, _winHeight / 5 + 2*_itemHeight+ _InternalHeight, _winWidth/ 2, _itemHeight), "授权码请联系轩科客服:13719347300", fontStyle);
			fontStyle.fontSize = 20;

			if (GUI.Button(new Rect(_startX, _winHeight / 5 + 3*_itemHeight + _InternalHeight, _winWidth / 5, _itemHeight), "sure"))
			{				
				if (key.Equals(_key))
				{					
					PlayerPrefs.SetInt("XKSecurity", 365+ _openDay);
					_isOpen = true;
					_isOpenSuccess = true;
					_isOpenfail = false;

				}
				else
				{
					_isOpenfail = true;

				}
			}		   
		}
		GUIStyle fontStyle2 = new GUIStyle();
		fontStyle2.fontSize = 40;
		fontStyle2.normal.textColor = new Color(1, 0, 0);
		if (_isOpenSuccess)
		{			
			GUI.Label(new Rect(_startX, _winHeight / 2, _winWidth / 2, _itemHeight), "授权成功:请重启!", fontStyle2);
		}
		
		if (_isOpenfail)
		{
			GUI.Label(new Rect(_startX, _winHeight * 0.8f, _winWidth / 2, _itemHeight), "授权码不对:请重新输入", fontStyle2);

		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值