栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 游戏开发 > Unity3D

外部程序控制应用——Unity/C# Process (Start,Kill等)

Unity3D 更新时间:发布时间: 百科书网 趣学号
1、启动外部程序

Process.Start();以及其多个重载;

using System.Diagnostics;
using System.Security;
using UnityEngine;

public class OpenProject : MonoBehaviour
{
    public void ClickOpenBtn()
    {
         //方法重载(1)
        //ProjectOpen();

        //方法重载(2)
        //ProjectOpen(ProcessStartInfo)方法示例
        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.FileName = "C:/Users/admin/Desktop/Test.exe";
        processStartInfo.Arguments = "61eaf8 9999 000";//每个参数之间需要空格进行分割
        ProjectOpen(processStartInfo);

        

        //方法重载(3)
        //ProjectOpen(fileName)方法示例
        //测试1:打开微信
        //string fileName = "C:/RG/WeChat/Setup/WeChat/WeChat.exe";
        //测试2:打开目标程序
        //string fileName = "C:/Users/admin/Desktop/Test.exe";
        //ProjectOpen(fileName);

        //方法重载(4)
        //Process.Start(name, parameters);方法示例
        //测试1:打开浏览器
        //string filePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";//Microsoft Edge浏览器
        //string paramters = "https://so.csdn.net/";
        //测试2:向被打开的应用传目标参数
        //string paramters = "OpenPanel";
        //执行方法
        //ProjectOpen(filePath, paramters);
    }

    private void ProjectOpen()
    {
        try
        {
            Process myprocess = new Process();
            string filePath = "C:/Users/admin/Desktop/Test.exe";
            string exeArgus = "test";
            ProcessStartInfo startInfo = new ProcessStartInfo(filePath, exeArgus);
            myprocess.StartInfo = startInfo;
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.Start();
            Application.Quit();
        }
        catch (Exception ex)
        {
            UnityEngine.Debug.LogFormat("出错原因:{0}", ex.Message);
        }
    }

    /// 
    /// 启动由包含进程启动信息的参数指定的进程资源,并将该资源与新的Process组件关联。
    /// 
    /// 启动信息的参数(例如,要启动的进程的文件名)
    private void ProjectOpen(ProcessStartInfo startInfo)
    {
        Process.Start(startInfo);
    }

    /// 
    /// 不带参数直接启动外部程序
    /// 
    /// 应用程序的名称,如:"C:/Users/admin/Desktop/Test/XXX.exe"
    private void ProjectOpen(string name)
    {
        Process.Start(name);
    }

    /// 
    /// 带命令行参数的启动一个外部应用程序
    /// 
    /// 应用程序的名称
    /// 一组命令行参数
    private void ProjectOpen(string name,string parameters)
    {
        Process.Start(name, parameters);
    }

    /// 
    /// 带用户信息参数的启动一个外部应用程序
    /// 
    /// 应用程序的名称
    /// 用户名
    /// 密码
    /// 域
    private void ProjectOpen(string name,string userName,SecureString userPass,string region)
    {
        Process.Start(name, userName, userPass, region);
    }

    /// 
    /// 带用户信息和一组命令行参数的启动一个外部应用程序
    /// 
    /// 应用程序的名称
    /// 一组命令行参数
    /// 用户名
    /// 密码
    /// 域
    private void ProjectOpen(string name,string paramters,string userName,SecureString userPass,string region)
    {
        Process.Start(name, paramters, userName, userPass, region);
    }
}
2、程序打开时接收传递过来的参数:

Environment.GetCommandLineArgs();获取传递过来的参数;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;

public class StartProject : MonoBehaviour
{
    public TMP_Text  text1;
    
    // 项目被打开运行时
    private IEnumerator Start()
    {
        yield return new WaitUntil(() => ApiConfig.instance.loaded);
#if UNITY_EDITOR //在编辑器环境下
#else //在打包后运行状态下
        //OpenProject1(); //也可正常使用
        OpenProject();                      
#endif
        yield break;
    }

    /// 
    /// 根据传给项目的参数,打开目标项目
    /// 
    private void OpenProject1()
    {
        string[] CommandLineArgs = Environment.GetCommandLineArgs();

        if (CommandLineArgs.Length < 2)
        {
            text1.text = "没有参数";
            Application.Quit();
        }
        else
        {
            if (CommandLineArgs[1] == "")
            {
                text1.text = "CommandLineArgs[1]值为空";
            }
            else
            {
                string[] args = Environment.GetCommandLineArgs();
                text1.text = args.Length.ToString();
                for (int i = 0; i < args.Length; i++)
                {
                    text1.text += "n" + "Arg" + i + ":  " + args[i];
                }
            }
        }
    }

    /// 
    /// 根据传给项目的参数,打开目标项目
    /// 
    private void OpenProject()
    {
        string[] CommandLineArgs = Environment.GetCommandLineArgs();
        //只有路径名一个参数
        if (CommandLineArgs.Length <= 1) Application.Quit();
        //有除了项目名/项目路径外的其他参数
        else
        {
            if (CommandLineArgs.Length == 5 && CommandLineArgs[4] == "OpenProject")
            {
                OpenProject.ProjectOption projectOption = new OpenProject.ProjectOption();
                projectOption.projectID = CommandLineArgs[1];
                projectOption.projectName = CommandLineArgs[2];
                projectOption.projectVersion = CommandLineArgs[3];
                ProjectManagement.instance.OpneProject(projectOption);
            }
        }
    }
}
3、关闭正在运行的自身程序:

Process.GetCurrentProcess().Kill(); 

//关闭自身
    private void KillCurProcess()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
            //#elif UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE_WIN
            //Application.Quit();
#else
            System.Diagnostics.Process.GetCurrentProcess().Kill();                      
#endif
    }
4、关闭外部程序时:使用 process.Kill();来关闭外部程序 ; (1)先获取进程/目标程序的进程名称

Process.GetCurrentProcess().MainModule.FileName;完整进程名;

Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName);进程名;

using TMPro;
using System;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using Newtonsoft.Json;
using System.Collections;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

...
...
...

private void GetCurProcessName()
    {
        //完整程序名称/地址,如:D:XXXYYYZZZMMM.exe
        string name1 = Process.GetCurrentProcess().MainModule.FileName;
        //仅程序名称,不加地址和后缀,这个name2,也就是执行关闭进程时,输入的processName,如:MMM;
        string name2 = Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName);
        //testText.text = string.Format("当前进程的进程名1:{0},进程名2:{1} ", name1, name2);
    }
(2)知道要关闭的进程名后,执行关闭操作

process.HasExited;进程是否退出;

process.Kill();关闭指定进程

private void KillProjectApplication(string processName)
    {
        Process[] processes = Process.GetProcesses();
        foreach (Process process in processes)
        {
            try
            {
                if (!process.HasExited && process.ProcessName == processName)
                    process.Kill();
            }
            catch (InvalidOperationException ex)
            {
                UnityEngine.Debug.Log(ex);
            }
        }
    }

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1068886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号