特殊原因,在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次");
? ? ? ? }
? ? }
}