Passing command line argument for screen resolution

Viewed 146

I am trying to open my Unity application through command prompt. I would like to pass on width and height so it could open in that resolution. I am using Screen.SetResolution() but I don't know how to pass the arguments:

// Use this for initialization
    void Awake()
    {
        Screen.SetResolution(h, w, false);
    }

Command line - UnityApp.exe -screen-width 800 -screen-height 570 -screen-fullscreen 0

1 Answers

You should be able to grab the applications command line arguments with the following using System.Environment.CommandLine & System.Environment.CommandLineArgs:

        string[] args = System.Environment.GetCommandLineArgs();
        int widthInput;
        int heightInput;
        for (int i = 0; i < args.Length; i++)
        {
            Debug.Log("ARG " + i + ": " + args[i]);
            if (args[i] == "-screen-width")
            {
                int.TryParse(args[i+1], out widthInput);
            } else if(args[i] == "-screen-height") {
                int.TryParse(args[i+1], out heightInput);
            }
        }
        if(heightInput != null && widthInput!=null)
            Screen.SetResolution(heightInput , widthInput, false);

Here is a link of the source code readjusted to suit your needs: https://answers.unity.com/questions/138715/read-command-line-arguments.html

Hope this helps!

Related