Unity打包后限制運行次數(shù)

特殊原因,在Unity 打包后,需要對運行的次數(shù)做一定的限制,這里用到C#的注冊列表的控制來達到限制的效果

以下代碼掛在Unity一個物體里就行了;如果是有切換場景的,記得需要掛在不銷毀的物體上

using UnityEngine;

using System.Collections;

using Microsoft.Win32;

public class SetUseTime : MonoBehaviour {

// Use this for initialization

void Start () {

? ? ? ? SetPlayUseTime();

? ? }

// Update is called once per frame

void Update () {

}

? ? void SetPlayUseTime()

? ? {

? ? ? ? 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"); ? ? ? //不存在,則創(chuàng)建子項

? ? ? ? ? ? RegKey = RootKey.OpenSubKey("TestToControlUseTime", true);

? ? ? ? ? ? RegKey.SetValue("UseTime", (object)3); ? ? ? ? ? ? ?//創(chuàng)建鍵值,存儲可使用次數(shù)

? ? ? ? ? ? return;

? ? ? ? }

? ? ? ? try

? ? ? ? {

? ? ? ? ? ? object usetime = RegKey.GetValue("UseTime"); ? ? ? ?//讀取鍵值,可使用次數(shù)

? ? ? ? ? ? print("還可以使用:" + usetime + "次");

? ? ? ? ? ? int newtime = int.Parse(usetime.ToString()) - 1;

? ? ? ? ? ? if (newtime < 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Application.Quit();

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? RegKey.SetValue("UseTime", (object)newtime); ? ?//更新鍵值,可使用次數(shù)減1

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? catch

? ? ? ? {

? ? ? ? ? ? RegKey.SetValue("UseTime", (object)3);

? ? ? ? ? ? print("更新使用3次");

? ? ? ? }

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,618評論 1 32
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,235評論 0 38
  • 一、基本數(shù)據(jù)類型 注釋 單行注釋:// 區(qū)域注釋:/* */ 文檔注釋:/** */ 數(shù)值 對于byte類型而言...
    龍貓小爺閱讀 4,431評論 0 16
  • About these tips(Edit: August 2016. I have revised these ...
    Francis_Rose閱讀 619評論 0 0
  • 英文文檔,一開始我也是抗拒的,邊翻譯邊看,也就花費了1個小時基本就閱讀過了,我的英文基礎其實很差。附上鏈接:鏈接:...
    lonecolonel閱讀 10,401評論 3 1

友情鏈接更多精彩內(nèi)容