Implementation of unity3d starting external program and passing parameters


Previous development projects have always used the mode of shell plus subroutine. The subroutine is started through the shell. The communication between the shell and the subroutine is completed by using the configuration file.

I always find such communication troublesome, because the shell needs to change and write the configuration file, and then the subroutine has to read the information. Moreover, during integration, there will be many configuration files, and many processing and restrictions need to be made on the path.

I found that process In the start() function, parameters can be passed.

In other words, we can use process When the start() function starts an external program, it passes parameters for communication.

The specific operations are as follows:

public void StartEXE()
        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.FileName = "C:/Users/Administrator/Desktop/Test/Demo.exe";
        processStartInfo. Arguments = "start program 1 2 3";

It should be noted that if there are multiple parameters, the parameters need to be separated by spaces.

The shell has passed parameters, so how does the subroutine accept parameters? The specific operations are as follows:

private void Start()
        string[] args = Environment.GetCommandLineArgs();
        text.text = args.Length.ToString();
        for (int i = 0; i < args.Length; i++)
            text.text += "\n" + "Arg" + i + ":  " + args[i];

I printed all the parameter information on a text. The operation effect diagram is as follows:

Supplement: unity3d: Start External exe, transfer parameters and set window position and size

I haven’t updated my blog for a long time. I haven’t made a big breakthrough in the recent project and didn’t involve new things, so I want to write a blog, but I have no new things. Fortunately, I have some new functions to do recently. I have done it before, but I haven’t sorted it into a blog. Let’s record it now. I have to go to Baidu to find it. (it seems that the new version of unity can’t be cracked recently. Sometimes the official website can’t be accessed. I don’t know what unity is going to do.)

Today, unity starts the external exe, passes parameters, and changes the position and size of the external exe window. Start exe Baidu search a lot, mainly how to set the window position and size. Unity has its own method for window size, but the location cannot be set. The method I use today is the native method of windows. Need to reference user32 dll。

No more nonsense. Here’s the code

using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics; 
public class ProperWindows : MonoBehaviour
    static extern IntPtr SetWindowLong(IntPtr hWnd, int _nIndex, int dwNewLong);
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    static extern IntPtr GetForegroundWindow(); // Gets the handle to the frontmost form
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    //static extern IntPtr GetWindowThreadProcessld(); 
    private void Awake()
        //The string array passed during startup. The subscript 0 is the complete path of the external exe program started, and the parameters subscript 1 and later are the parameters you want to pass.
        string[] args = Environment.GetCommandLineArgs();
        var winInfo = JsonUtility.FromJson<WinInfo>(args[1]);
        //Set screen size and display position
        SetWindowPos(GetForegroundWindow(), 0, winInfo.x, winInfo.y, winInfo.width, winInfo.height, 0x0040);
    // Use this for initialization
    void Start()
        //Start the external exe program. The first parameter is the full path of exe and the second parameter is the parameter to be passed in.
        string winInfo = JsonUtility.ToJson(new WinInfo(0, 0, 1000, 500));
        Process.Start(@"C:\Users\wangbo\Desktop.exe", winInfo);
    // Update is called once per frame
    void Update()
public class WinInfo
    public int x;
    public int y;
    public int width;
    public int height;    
    public WinInfo(int x, int y, int width, int height)
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

In the above code, the parameters I pass are in JSON format. Start an EXE in start, receive parameters in wake, and set the window position and size.

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.