C#杀死unity中的进程方法
Published in:2023-03-02 |
Words: 310 | Reading time: 1min | reading:

unity 退出 app

1
2
3
4
5
6
7
/// <summary>
/// exit
/// </summary>
public void ExitUnity()
{
Application.Quit();
}

unity 暂停退出

1
2
3
4
5
6
7
/// <summary>
/// unload
/// </summary>
public void UnloadUnity()
{
Application.Unload();
}

unity 杀死当前所有进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <summary>
/// 退出方式1 https://blog.csdn.net/sakuya_miku/article/details/109219878
/// </summary>
void OnExitBtnClick(string processName)
{
// https://blog.csdn.net/qq_38721111/article/details/82864423
UnityEngine.Debug.Log("当前应用:" + System.Diagnostics.Process.GetCurrentProcess().ProcessName + " 进程ID: " + System.Diagnostics.Process.GetCurrentProcess().Id);
// ListAllAppliction();
UnityEngine.Debug.Log("On Exit Btn CLick method executing");
try
{

System.Diagnostics.Process.GetCurrentProcess().Exited += new EventHandler(exep_Exited);
System.Diagnostics.Process.GetCurrentProcess().CloseMainWindow();
System.Diagnostics.Process.GetCurrentProcess().Close();
}
catch (Exception e)
{
UnityEngine.Debug.Log("error : " + e);
}
}

void exep_Exited(object sender, EventArgs e)
{
UnityEngine.Debug.LogError("CloseMainWindow");
}

unity 杀死所有进程

1
2
3
4
5
6
7
8
9
10
11
12
/// <summary>
/// 杀死进程
/// </summary>
/// <param name="processName">应用程序名</param>
void KillProcess(string processName)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
for (int i = 0; i < processes.Length; i++)
processes[i].Kill();
UnityEngine.Debug.Log("已杀死进程");
}

unity 通过 Pid 杀死指定进程

1
2
3
4
5
6
7
8
9
10
/// <summary>
/// kill by pid
/// </summary>
/// <param name="id"></param>
void KillByPid(string id)
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(int.Parse(id));
p.Kill();

}

杀死当前单一进程

1
2
3
4
5
6
7
/// <summary>
/// test single kill process
/// </summary>
void SingleKillProcess()
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
Prev:
百度地图功能使用
Next:
Android 在Activity中打开某个app