Unity设置自启动项目

今天在蛮牛上看到一篇设置unity导出的程序自启动项目,感觉还不错,源码如下:

using UnityEngine;
using System.Collections;
using Microsoft.Win32;
using UnityEngine.UI;

public class test : MonoBehaviour
{

    public Button Btn1;
    public Button Btn2;
    public Text text;

    void OnEnable()
    {
        Btn1.onClick.AddListener(OnBtn1Click);
        Btn2.onClick.AddListener(OnBtn2Click);
    }

    void OnDisable()
    {
        Btn1.onClick.RemoveListener(OnBtn1Click);
        Btn2.onClick.RemoveListener(OnBtn2Click);
    }
    private void OnBtn1Click()
    {
        //MessageBox.Show("设置开机自启动,需要修改注册表", "提示");
        try
        {
            string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (rgkRun == null)
            {
                rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            }
            rgkRun.SetValue("dhstest", path);
        }
        catch
        {
            Debug.Log(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
       
        }
         finally
        {
            regeditkey();
            Debug.Log(123);
        }
    }

    private void OnBtn2Click()
    {
        //MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
        try
        {
            string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                true);
            if (rgkRun == null)
            {
                rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            }
            rgkRun.DeleteValue("dhstest", false);
        }
        catch
        {
            Debug.Log("error");
        }
        finally
        {
            regeditkey();
            Debug.Log(123);
        }

    }




    public void regeditkey()
    {
        Debug.Log(123);
        RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        if (rgkRun.GetValue("dhstest") == null)
        {
            text.text = "自启动为关闭";
        }
        else
        {
            text.text = "自启动为打开";
            
        }
    }


}
 


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Unity 中,可以使用 AndroidManifest.xml 文件来配置应用程序的行为和属性。要使应用程序在 Android 设备启动时自动运行,可以在 AndroidManifest.xml 文件中添加以下代码: ```xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application android:name="UnityPlayerActivity" android:icon="@drawable/app_icon" android:label="@string/app_name" android:theme="@style/UnityThemeSelector"> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:screenOrientation="landscape" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26" /> <uses-feature android:glEsVersion="0x00020000" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest> ``` 其中,`UnityPlayerActivity` 指定了 Unity 的主 activity,`android.intent.action.MAIN` 和 `android.intent.category.LAUNCHER` 指定应用程序作为主 activity,并将其添加到 Android 设备的应用程序列表中。`android:launchMode="singleTask"` 指定 activity 的启动模式为 `singleTask`,这意味着只能有一个实例存在。 此外,确保将 `AndroidManifest.xml` 文件添加到 Unity 项目中,并将 `Build Settings` 中的 `Target Platform` 设置为 `Android`。最后,使用 Unity 的 `Build and Run` 命令将应用程序构建为 Android 包,然后在 Android 设备上安装和启动它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值