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

相关推荐
在路上看风景6 小时前
31. Unity 异步加载的底层细节
unity
天人合一peng8 小时前
Unity中做表头时像work中整个调整宽窄
unity
小李也疯狂20 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的20 小时前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y20 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤20 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里20 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ21 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
YigAin1 天前
Unity中的Lock,到底在锁什么,什么时候该用?
unity
Var_al1 天前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl