代码改变世界

Unity 定时开启/关闭外部应用

2015-10-31 15:09  wuzhang  阅读(3919)  评论(1编辑  收藏  举报

  自从加入工作的队伍里,博客都荒废了,今天周末,难得清静,写篇博客!刚才看到了Process类,随手写了个小demo.给大家分享下!

首先大家要对Process类有一些简单的了解,参考资料:https://msdn.microsoft.com/zh-cn/library/system.diagnostics.process%28v=vs.110%29.aspx 如果有兴趣可以深入学习。学习过后,我们大家先要写一些挂机软件,分分钟就能搞定了!如果你编程更厉害可以写一些游戏外挂什么的,当然不建议大家这么做,自己写着玩还是很有意思的!

1,打开Uinity 建一新工程。

2.创建脚本文件 CallApplication.cs

using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System;

public class CallApplication : MonoBehaviour
{

	// Use this for initialization
	private static string outputPath = "D:/kuwo/kuwomusic/8.0.3.0_BCS30/KwMusic.exe";
	private int x = 0, y = 0;
	private bool isOn = false;
	private int minute = 0;

	// Use this for initialization
	void Start()
	{
		//ListAllAppliction();
		UnityEngine.Debug.Log("当前应用:"  + Process.GetCurrentProcess().ProcessName+" 进程ID: "+Process.GetCurrentProcess().Id);
	}
	void OnGUI()
	{
		if (GUI.Button(new Rect(10, 10, 200, 50), "打开外部应用"))
		{
			if (CheckProcess("kwmusic"))
				return;
			else
				StartProcess(outputPath);
		}
		if (GUI.Button(new Rect(10, 60, 200, 50), "杀死应用进程"))
		{
			KillProcess("kwmusic");
		}
		if (GUI.Button(new Rect(10, 110, 200, 50), "开启定时关闭"))
		{
			minute = System.DateTime.Now.Minute + 1;
			isOn = true;
		}
		if (isOn)
		{
			GUI.contentColor = Color.red;
			GUI.Label(new Rect(10, 160, 160, 30), "\t倒计时:" + (60 - System.DateTime.Now.Second));
			if (System.DateTime.Now.Minute == minute)
			{
				UnityEngine.Debug.Log("自动关闭应用....");
				KillProcess("kwmusic");
				isOn = false;
			}
		}
	}
	/// <summary>
	/// 开启应用
	/// </summary>
	/// <param name="ApplicationPath"></param>
	void StartProcess(string ApplicationPath)
	{
		UnityEngine.Debug.Log("打开本地应用");
		Process foo = new Process();
		foo.StartInfo.FileName = ApplicationPath;
		foo.Start();
	}

	/// <summary>
	/// 检查应用是否正在运行
	/// </summary>
	bool CheckProcess(string processName)
	{
		bool isRunning = false;
		Process[] processes = Process.GetProcesses();
		int i = 0;
		foreach (Process process in processes)
		{
			try
			{
				i++;
				if (!process.HasExited)
				{
					if (process.ProcessName.Contains(processName))
					{
						UnityEngine.Debug.Log(processName + "正在运行");
						isRunning = true;
						continue;
					}
					else if (!process.ProcessName.Contains(processName)&&i>processes.Length)
					{
						UnityEngine.Debug.Log(processName + "没有运行");
						isRunning = false;
					}
				}
			}
			catch (Exception ep)
			{
			}
		}
		return isRunning;
	}
	/// <summary>
	/// 列出已开启的应用
	/// </summary>
	void ListAllAppliction()
	{
		Process[] processes = Process.GetProcesses();
		int i = 0;
		foreach (Process process in processes)
		{
			try
			{
				if (!process.HasExited)
				{
					UnityEngine.Debug.Log("应用ID:"+process.Id+"应用名:"+process.ProcessName);
				}
			}
			catch (Exception ep)
			{
			}
		}
	}
	/// <summary>
	/// 杀死进程
	/// </summary>
	/// <param name="processName">应用程序名</param>
	void KillProcess(string processName)
	{
		Process[] processes = Process.GetProcesses();
		foreach (Process process in processes)
		{
			try
			{
				if (!process.HasExited)
				{
					if (process.ProcessName == processName)
					{
						process.Kill();
						UnityEngine.Debug.Log("已杀死进程");
					}
				}
			}
			catch (System.InvalidOperationException)
			{
				//UnityEngine.Debug.Log("Holy batman we've got an exception!");
			}
		}
	}

}

 

3.将脚本挂到摄像机上即可。

4.Ctrl+P走你!

 

检查当前打开的进程(系统应用除外),获取到之后,怎么操作It's up to you!

5,接着看受否可以启动外部应用,我就以酷我音乐盒为例吧!

还真的打开了啊!

6,试试关闭它吧。

 

真的关了,再次打开。

 

再试试定时关闭,1分钟测试时间!

已经OK了吧!

下面简单的看下代码,主要就有这几个方法:

大家可以自己去扩充方法!