Unity打包后限制运行次数

来源:互联网 发布:js正则表达式匹配空格 编辑:程序博客网 时间:2024/05/22 01:45

特殊原因,在Unity 打包后,需要对运行的次数做一定的限制,这里用到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");       //不存在,则创建子项
            RegKey = RootKey.OpenSubKey("TestToControlUseTime", true);
            RegKey.SetValue("UseTime", (object)3);              //创建键值,存储可使用次数
            return;
        }
        try
        {
            object usetime = RegKey.GetValue("UseTime");        //读取键值,可使用次数
            print("还可以使用:" + usetime + "次");
            int newtime = int.Parse(usetime.ToString()) - 1;
            if (newtime < 0)
            {
                Application.Quit();
            }
            else
            {
                RegKey.SetValue("UseTime", (object)newtime);    //更新键值,可使用次数减1
            }
        }
        catch
        {
            RegKey.SetValue("UseTime", (object)3);
            print("更新使用3次");
        }
    }
}

1 0