Unity打包后限制运行次数

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

        }

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,141评论 1 32
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,905评论 0 38
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,288评论 0 16
  • About these tips(Edit: August 2016. I have revised these ...
    Francis_Rose阅读 559评论 0 0
  • 英文文档,一开始我也是抗拒的,边翻译边看,也就花费了1个小时基本就阅读过了,我的英文基础其实很差。附上链接:链接:...
    lonecolonel阅读 10,009评论 3 1