Unity | 调用外部exe

一、调用另一个Unity EXE软件

    /// <summary>
    /// 打开外部程序
    /// </summary>
    /// <param name="_exePathName">EXE所在绝对路径及名称,带后缀.exe</param>
    /// <param name="_exeArgus">启动参数</param>
    public static void StartEXE(string _exePathName, string _exeArgus)
    {
        try
        {
            System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(@_exePathName, _exeArgus);
            myprocess.StartInfo = startInfo;
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.Start();
        }
        catch (Exception ex)
        {
           Debug.Log("加载exe失败," + ex.Message);
        }
    }

二、调用控制台应用程序

加载控制台应用程序需要设置WorkingDirectory属性(猜测原因:我项目中加载的控制台应用程序启动时要调用别的dll,这个dll要加载本地模型文件,所以需要设置exe的当前工作目录),否则会加载失败。

    /// <summary>
    /// 打开外部程序
    /// </summary>
    /// <param name="_exePathName">EXE所在绝对路径,不带名称</param>
    /// <param name="_exeName">exe名称,带后缀</param>
    public static void StartEXE(string _exePathName,string _exeName)
    {
        try
        {
            System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(@_exePathName+ "/"+_exeName);
            myprocess.StartInfo = startInfo;
            myprocess.StartInfo.WorkingDirectory = @_exePathName;
            myprocess.StartInfo.CreateNoWindow = true;
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.StartInfo.RedirectStandardOutput = true;
            myprocess.Start();
        }
        catch (Exception ex)
        {
            Debug.Log("加载exe失败," + ex.Message);
        }
    }

三、关闭进程

    /// <summary>
    /// 杀死进程
    /// </summary>
    /// <param name="_exePathName">进程名称,不带路径及后缀</param>
    public static void CloseExe(string _exePathName)
    {
        System.Diagnostics.Process[] progress = System.Diagnostics.Process.GetProcessesByName(_exePathName);
        Debug.Log("progress个数:" + progress.Length);
        for (int i = 0; i < progress.Length; i++)
        {
            progress[i].Kill();
        }
    }

四、路径加载注意事项

如果第三方exe的路径是通过config.txt配置的,那么读取这个路径字符串时需要注意:

  1. 方式一

路径字符串单独一行。那么读取字符串时要注意其length比实际字符串多1位,所以要进行字符串处理。

  • 配置文件

C:\Users\fengchujun\Desktop\zhanhuiexe\pipiGame\PipiDriver.exe
  • 读取路径字符串

  IEnumerator GetConfigData()
    {
        using (UnityWebRequest w = UnityWebRequest.Get(Application.streamingAssetsPath + "/configV2.1.0.txt"))
        {
            yield return w.SendWebRequest();
            if (w.isNetworkError || w.isHttpError)
            {
                Debug.Log("GetConfig error");
            }
            else
            {
                string data = w.downloadHandler.text;
                string[] _array = data.Split('\n', System.StringSplitOptions.RemoveEmptyEntries);
              
                ExePath=_array[0];
            }
        }
    }
  • 字符串处理及调用

 if (ExePath != "")
 {
      string  path = @ExePath.Substring(0,ExePath.Length-1);
      Debug.Log(path.Length+":"+ path);
      StartEXE(path, null);
 }
  1. 方式二

路径按空格分割的方式和别的数据在一行,那么这个路径字符串不需要处理。

  • 配置文件

(注意exe后有空格)

1080 720 25 2 5 14 17 C:\Users\fengchujun\Desktop\zhanhuiexe\pipiGame\PipiDriver.exe 
  • 读取路径字符串

 IEnumerator GetConfig()
  {
        using (UnityWebRequest w = UnityWebRequest.Get(Application.streamingAssetsPath + "/config.txt"))
        {
            yield return w.SendWebRequest();
            if (w.isNetworkError || w.isHttpError)
            {
                Debug.Log("GetConfig error");
            }
            else
            {
                string info = w.downloadHandler.text;
                string[] _array = info.Split('\n', System.StringSplitOptions.RemoveEmptyEntries);
                Debug.Log(_array.Length);

                //get cameraInfo
                string[] cameraInfo = _array[0].Split(' ', System.StringSplitOptions.RemoveEmptyEntries);
                Debug.Log(cameraInfo.Length);
                //...
                softExePath= cameraInfo[7];
            }
       } 
}
  • 调用

    /// <summary>
    /// 开启第三方exe
    /// </summary>
    /// <returns></returns>
    void StartExe()
    {
        CommonData.WriteIntoTxt("第三方软件exe:" + softExePath);
        if (softExePath != "")
        {
            CommonData.StartEXE(softExePath, null);
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_39766005/article/details/129324489