Unity 限時使用 限制試用時間和使用次數


//***************************************************
//作用:限制使用時間或次數
//用法:時間限制,修改Start()函數里的方法即可
//SetPlayUseNumber()為限制次數方法,修改鍵值名就可以重新計算("UseTime")
//
//
//***************************************************
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SetUserTime : MonoBehaviour {

    //注冊名
    string UseTime = "UseTime15";
    //試用次數
    int num = 1100;
    //到期提示文字
    public Button Btn_expire;

    // Use this for initialization
    void Start () {
        //===(比如從3月1日開始計算,到4月1日結束)
        //只要小於minTime時間或大於maxTime時間,將停止使用
        DateTime minTime = Convert.ToDateTime("2020-7-1");
        DateTime maxTime = Convert.ToDateTime("2020-8-1");
        if (minTime > DateTime.Now || DateTime.Now > maxTime)
        {
            //===到期后,超時提示
            UseTime = "到期了";
            num = 0;
            print("到期了");
        }

        //===本方法放在此處,可以實現時間和次數同時限制
        SetPlayUseNumber();


        //if (minTime < DateTime.Now && DateTime.Now < maxTime)
        //{
        //    //===用於限時功能
        //    //在這段時間內能使用xx功能
        //}
    }

    /// <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("ScaffoldProjectToControlUseTime", true)) == null)
        {
            RootKey.CreateSubKey("ScaffoldProjectToControlUseTime");       //不存在,則創建子項
            RegKey = RootKey.OpenSubKey("ScaffoldProjectToControlUseTime", true);
            RegKey.SetValue(UseTime, (object)num);              //創建鍵值,存儲可使用次數
            print("實例化");
            return;
        }
        try
        {
            object usetime = RegKey.GetValue(UseTime);        //讀取鍵值,可使用次數
            print("還可以使用:" + usetime + "");
            int newtime = int.Parse(usetime.ToString()) - 1;//可使用次數減1
            if (newtime < 0)
            {
                //到期退出程序
                RegKey.SetValue(UseTime, (object)newtime);//20190225添加
                Btn_expire.gameObject.SetActive(true);
                //Btn_expire.gameObject.transform.GetChild(0).GetComponent<Text>().text="試用已到期,請聯系管理員";

                Invoke("OnQuit", 3);//延時退出
            }
            else
            {
                RegKey.SetValue(UseTime, (object)newtime);    //更新鍵值,可使用次數減1
                //Btn_expire.gameObject.SetActive(false);

            }
        }
        catch
        {
            RegKey.SetValue(UseTime, (object)num);
            print("首次進入,更新使用次數");
        }
    }

    private void OnQuit()
    {
        Application.Quit();
    }
}

 

【時間限制】修改Start()函數里的minTime和maxTime時間即可。限制時間也可精確到秒,比如:

DateTime minTime = Convert.ToDateTime("2019-4-23 12:22:05");
【次數限制】SetPlayUseNumber()為限制次數方法,修改鍵值名就可以重新計算("UseTime")

本腳本是限制時間和次數的搭配使用,可自行修改。

注:每次到期后(次數和時間),UseTime的值要變


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM