Process.Start解决开机启动问题

    xiaoxiao2021-03-25  119

    问题 每天开机都会打开几个必要的程序,比如QQ、unity、浏览器这些,如果手动打开不仅没效率还影响心情,在开机启动里设置启动项是个不错的办法,但是过一段时间就会提示xxx.dll丢失,xxx.exe启动异常。。。 解决 利用UnityEngine.Application .OpenURL 或者System.Diagnostics.Process.Start来把这种风险降至最低。比如我平时要启动四个程序,现在只需要启动一个,大大降低了发生此错误的概率…

    using UnityEngine; using UnityEngine.UI; using System.Collections; public class FontTest : MonoBehaviour { public Text text;//输入文本 public Text text2;//展示、提示文本 public Text text3;//提示文本 private GameObject button; private Transform canvas; int shiftY = 450; private IEnumerator Start() { button = Resources.Load("Button") as GameObject; canvas = GameObject.Find("Canvas").transform; index = PlayerPrefs.GetInt("Key"); if (index == 0) { PlayerPrefs.SetInt("Key", 0); text3.text = "没有可执行的任务"; } else { Debug.Log(index); text3.text = "本次执行的任务如下:\n"; text2.text = null; for (int i = 1; i < index + 1; i++) { yield return new WaitForSeconds(1); string filePath = PlayerPrefs.GetString(i.ToString()); System.Diagnostics.Process.Start(filePath); text2.text += PlayerPrefs.GetString(i.ToString()) + "\n"; GameObject clone = Instantiate(button); Button mb = clone.GetComponent<Button>(); mb.transform.SetParent(canvas); mb.gameObject.transform.position = new Vector3(990, shiftY, 0); shiftY -= 35; mb.GetComponentInChildren<Text>().text = filePath.Substring(filePath.LastIndexOf('/') + 1); string _key = i.ToString(); mb.onClick.AddListener(delegate { RemoveKey(_key, mb.gameObject); }); } } } public void RemoveKey(string key, GameObject game) { PlayerPrefs.DeleteKey(key); index--; PlayerPrefs.SetInt("Key", index); Debug.Log(game.name); text3.text = "已经移除------" + game.GetComponentInChildren<Text>().text; game.GetComponentInChildren<Button>().interactable = false; } private int index = 0; public void AddMisson() { int mIndex = PlayerPrefs.GetInt("Key"); mIndex++; string filePath = text.text.Replace("\\", "/"); PlayerPrefs.SetString(mIndex.ToString(), filePath); PlayerPrefs.SetInt("Key", mIndex); text3.text = "成功添加------" + filePath.Substring(filePath.LastIndexOf('/') + 1); GameObject clone = Instantiate(button); Button mb = clone.GetComponent<Button>(); mb.transform.SetParent(canvas); mb.gameObject.transform.position = new Vector3(990, shiftY, 0); shiftY -= 35; mb.GetComponentInChildren<Text>().text = filePath.Substring(filePath.LastIndexOf('/') + 1); string _key = PlayerPrefs.GetInt("Key").ToString(); Debug.Log(_key); mb.onClick.AddListener(delegate { RemoveKey(_key, mb.gameObject); }); } public void ClearList() { PlayerPrefs.DeleteAll(); text3.text = "已经清空完毕,请重新指定任务"; } public void ExitGame() { Application.Quit(); } }

    写完运行之后呢,打开是这个样子: addtask是添加一个任务,在下边框写入文件路径, cleartask是清空所有的任务, exit是退出程序, 每创建一个任务会生成对应的button,点击button会取消对应的任务

    转载请注明原文地址: https://ju.6miu.com/read-8235.html

    最新回复(0)