Unity应用开机自启动

使用说明

以代码设置的方式设置Unity应用开机自启动。

将下面脚本挂载到场景物体,通过UI按钮开启 应用自启动和取消 应用自启动,设置下次运行应用生效

所用到的Dll下载地址:Interop.IWshRuntimeLibrary

脚本代码

csharp 复制代码
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using IWshRuntimeLibrary;

public class ProjectStartMenu : MonoBehaviour
{
    public Button setupStartupButton;
    public Button cancelStartupButton;
    public Text hintText;
   
    private static string ShortcutName = "test.lnk";//快捷方式名称,可改成你项目名称

    private void OnEnable()
    {
        isStartup();
        setupStartupButton.onClick.AddListener(OnSetupStartupButtonClick);
        cancelStartupButton.onClick.AddListener(OnCancelStartupButtonClick);
    }

    private void OnDisable()
    {
        setupStartupButton.onClick.RemoveListener(OnSetupStartupButtonClick);
        cancelStartupButton.onClick.RemoveListener(OnCancelStartupButtonClick);
    }

    private void OnSetupStartupButtonClick()
    {
       // ShortcutName = UnityEditor.PlayerSettings.productName;
        CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup), ShortcutName, System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
        isStartup();
    }

    private void OnCancelStartupButtonClick()
    {
        if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + ShortcutName))
            System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + ShortcutName);
        isStartup();
    }

    private void isStartup()
    {
        if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + ShortcutName))
            hintText.text = "应用当前已开机自启";
        else
            hintText.text = "应用当前非开机自启";
    }

    public static bool CreateShortcut(string direstory, string shortcurName, string targetPath, string description = null, string iconLocation = null)
    {

        try
        {
            if (!Directory.Exists(direstory))
            {
                Directory.CreateDirectory(direstory);
            }
            string shortscurPath = Path.Combine(direstory, string.Format("{0}", shortcurName));
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortscurPath); // 创建快捷方式对象
            shortcut.TargetPath = targetPath; // 指定目标路径
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //设置起始位置
            shortcut.WindowStyle = 1; // 设置运行方式,默认为常规窗口
            shortcut.Description = description; // 设置备注
            shortcut.IconLocation = string.IsNullOrEmpty(iconLocation) ? targetPath : iconLocation; //设置图标路径
            shortcut.Save(); // 保存快捷方式
            return true;
        }
        catch
        {

        }
        return false;
    }
}

Unity截图:




通过cmd的shell:startup可查看是否创建成功

C:\Users\xxx\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

参考博客:https://blog.csdn.net/m0_46385244/article/details/128253428

相关推荐
孟无岐11 小时前
【Laya】LocalStorage 本地存储
typescript·游戏引擎·游戏程序·laya
妙为19 小时前
unreal engine5角色把敌人 “挤飞”
游戏引擎·虚幻·ue·unrealengine5
4Forsee19 小时前
【增强现实】快速上手 Vuforia Unity Android AR 应用开发
android·unity·ar
两水先木示19 小时前
【Unity】对指定物体进行描边——模板测试法
unity·游戏引擎·shader·外描边
Miss_SQ20 小时前
实现Unity录音、百度云语音转文字
unity·语音识别
CreasyChan20 小时前
unity 对象池实测可用
unity·c#
weixin_4242946720 小时前
Unity项目的Artifacts文件夹过大怎么解决?
unity·游戏引擎
没事写写笔记1 天前
Unity HDRP14.0.12 Volume 配置参数
unity
红黑色的圣西罗1 天前
手游手动异形屏适配方案,类“明日方舟”
unity
syker2 天前
3D游戏引擎Bluely Engine 开发手册
开发语言·3d·游戏引擎