Problem with commandline arguments can't parse - width and height?

Hello everyone, I want build own script like any game has feature of commandline arguments like this game.exe -w 1024 -h 768 -window …

I have tried - but unityplayer starts default resolution than I want call custom arguments

using System;
using UnityEngine;

public class CommandLine : MonoBehaviour {

    string[] args = Environment.GetCommandLineArgs();
    Resolution resol = Screen.currentResolution;
    // Use this for initialization
    void Start () {
        int argIndex = args.Length;

        for (int i = 0; 1 < argIndex; i++)
        {
            switch (args[i])
            {
                case "-width":
                case "-w":
                    resol.width = int.Parse(args[++i]);
                    break;

                case "-height":
                case "-h":
                    resol.height = int.Parse(args[++i]);
                    break;

                default:
                    Screen.SetResolution(640, 480, false);
                    break;
            }
        }
    }
}

Than It shows like normal:

But I tried with powershell or command:


If I call test.exe -w 1024 -h 768 than it doesn’t show correct.

How do I fix current resolution of unity player.

PS: Community can not replace :frowning:

3205248–245139–example_unityplayer_with_arguments.zip (2.99 KB)

Resolve it! But it has problem if I change width and height than it looks like crazy. How do I fix if I write current width and current height?

Possible OnInitalize or OnChange of void?

Thanks you never answer me Oh…

using System;
using UnityEngine;

public class CommandLine : MonoBehaviour {

    string[] args = Environment.GetCommandLineArgs();
    string mWidth = "";
    string mHeight = "";

    // Use this for initialization
    void Start () {
        for (int i = 0; i < args.Length; i++)
        {
            if (args[i] == "-width" || args[i] == "-w")
            {
                mWidth = args[i + 1];
            }

            if (args[i] == "-height" || args[i] == "-h")
            {
                mHeight = args[i + 1];
            }
        }
    }

    private void OnGUI()
    {
        Screen.SetResolution(int.Parse(mWidth), int.Parse(mHeight), false);
    }
}

Result:
Normal: Start executable

Advanced: Start executable with arguments


But it is really crazy if I write current width and current height than it start to resize to new width and new height. It is wrong.

Any resolves? void Updated() { …} and void OnGUI() { … } still have always resizing mode. I really want show fast width and height like any games with arguments

Please help me!

If it doesn’t show any feature for fast current width and current height.

I can’t find any actions of Unity MonoBehaviour
Like OnInitialze or ApplicationComplete, OnResize etc for unity player’s window?

It resizes when it does because none of your code executes until after the splash screen is finished. If you save the height and width to PlayerPrefs then at the very least it will use the last resolution it opened with but otherwise you’re stuck.

Ah okay thanks for explanation!

You mean that my example of commandline arguments width and height still work fine?

I thought I tried example OpenTK 2.00 / 3.00 Preview and it shows normal without resizing mode. It is so impossible because Unity has maybe bug? I will tell you an example of OpenTK I already write that

For example:
hlSharp.exe is launcher
Engine.dll ( just arguments with width and height and any arguments )
PS: Why does Unity Player not recognize switch-case statements ? Only If-else statements? It looks like very old parameters by MDM Zinc Studio / Adobe Air.

I show example codes with OpenTK:
For Engine.dll

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using System;
using System.Drawing;
using System.IO;
using System.Windows;

namespace Engine
{
    public class Client
    {
        private SDLWindow game;
        bool developer = false;
        bool showConsole = false;
        string gameModDir;

        private Point CenterWindowOnScreen()
        {
            int screenWidth = (int)SystemParameters.PrimaryScreenWidth;
            int screenHeight = (int)SystemParameters.PrimaryScreenHeight;
            int gameWidth = game.Width;
            int gameHeight = game.Height;
            return new Point((screenWidth / 2) - (gameWidth / 2), (screenHeight / 2) - (gameHeight / 2));
        }

        public int Host(string[] args)
        {
            int argIndex = args.Length;
            game = new SDLWindow();;
            for (int i = 0; i < argIndex; i++)
            {
                switch (args[i])
                {
                    case "-width":
                    case "-w":
                        game.Width = int.Parse(args[++i]);
                        break;

                    case "-height":
                    case "-h":
                        game.Height = int.Parse(args[++i]);
                        break;

                    case "-game":
                        if(gameModDir == AppDomain.CurrentDomain.BaseDirectory + args[++i])
                        {
                            if(File.Exists(gameModDir))
                                gameModDir = AppDomain.CurrentDomain.BaseDirectory + args[++i];
                        } else
                        {
                            gameModDir = AppDomain.CurrentDomain.BaseDirectory + "valve";
                            game.Title = "Half-Life CSharp";
                        }
                        break;

                    case "-noborder":
                        game.WindowBorder = WindowBorder.Hidden;
                        break;

                    case "-dev":
                        developer = true;

                        break;

                    case "-console":
                        showConsole = true;

                        break;

                    default:
                        break;
                }
            }
            game.Location = CenterWindowOnScreen();
            game.Run(60);
            return argIndex;
        }
    }

    class SDLWindow : GameWindow
    {
        public SDLWindow() : base(640, 480, GraphicsMode.Default,
            "Half-Life CSharp", GameWindowFlags.FixedWindow,
            DisplayDevice.Default, 4, 0, GraphicsContextFlags.Default)
        {
            WindowBorder = WindowBorder.Fixed;
        }

        protected override void OnResize(EventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);
        }

        protected override void OnLoad(EventArgs e)
        {
            CursorVisible = true;
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            Color4 backColor;
            backColor.A = 1.0f;
            backColor.R = 0.1f;
            backColor.G = 0.1f;
            backColor.B = 0.3f;
            GL.ClearColor(backColor);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            SwapBuffers();
        }
    }
}

And launcher of HLSharp with OpenTK

using System;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

namespace hlSharp
{
    class Program
    {
        /**
         *  args should "-dev, -console, -width, -height
         *  and more paraments of original Half-Life"
         */
        static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");


        static int Main(string[] args)
        {
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                var DLL = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "Engine.dll");
                var class1Type = DLL.GetType("Engine.Client");
                var c = Activator.CreateInstance(class1Type);
                var method = class1Type.GetMethod("Host");
                method.Invoke(c, new object[] { args });

                mutex.ReleaseMutex();
            }
            else
            {
                MessageBox.Show("Please close instance of Half-Life CSharp!", "HL-CSharp Message");
            }
            return 0;
        }
    }
}

Result:


But it works fine. ( Sometimes crash - I can not find resolve )

// Fixed version because I forget to restart computer because VS Community 2017 shows built-in apps but without VS Community 2017 it can not show app ( it makes invisible and close. That is why It resolves fine. PS: Thanks for explanation because Unity Player has sure bug - if I use commandline arguments than it shows “resizable mode” - But It is really crazy. That is why I wish unity player should work fine like my opentk-game with arguments. Please fix for next version with unity player. Where is report bug tracker of Unity? I am sure because they say if unity player doesn’t work if I use commandline arguments than it makes as window with resizing mode. - I really don’t like that. Please believe me!

I show you my written gamelauncher with changing resolution:

// EDIT:
I found report bug from Unity Editor. I have submitted it.

And I already upgrade to 2017.1.0p5 and it has nothing fix :frowning: Please fix Unity player’s window without resizing mode. :frowning:

And I have problem with bool if I use commandline argument “-windowed” than it will enable as windowed unity player but it doesn’t work.

using System;
using UnityEngine;

public class StartupScript : MonoBehaviour {

    string[] args = Environment.GetCommandLineArgs();
    string jWidth = ""; // unity player width #
    string jHeight = ""; // unity player height #
    string jWindowed = ""; // unity player windowed

    // Use this for initialization
    void Start () {
        for (int i = 0; i < args.Length; i++)
        {
            if (args[i] == "-width" || args[i] == "-w")
            {
                jWidth = args[i + 1];
            }

            if (args[i] == "-height" || args[i] == "-h")
            {
                jHeight = args[i + 1];
            }

            if (args[i] == "-windowed")
            {
                jWindowed = args[i];
            }
        }
    }
   
    // Update is called once per frame
    void Update () {
        Screen.SetResolution(int.Parse(jWidth), int.Parse(jHeight), bool.Parse(jWindowed));
        if (jWindowed == null)
        {
            Screen.fullScreen = true;
        }
        else
        {
            Screen.fullScreen = false;
        }
    }
}

Please tell me - how do I use only parameter without true or false. just single parameter like -windowed or -dev -console etc

Like any games have launch with parameters

It’s not a bug. It’s how Unity launches its player.

switch blocks work just fine in Unity.

The toggle you have for Screen.fullScreen should be unnecessary.

You’re looking for the existing command line argumens that the player takes into account when it’s launched. (Scroll down for the standalone args).

Specifically

  1. -screen-width …
  2. -screen-height …

These can be used to launch the player with your desired size, but I think you’ll need to disable the resolution dialog.
If you need shorter names, try to write a batch which allows to feed in arguments using different names, such as -w | -h and launch the application with the actual parameter names.

1 Like

Hi Sorry for delay… how do i replace from unity player args?
You mean procress with arguments? Or I understand wrong.
// EDIT:

You mean I need add passing parameter with catched unity player’s argument. I really don’t understand I already tried.

How do I know if I use short argument like -w or -h?
Can you write an example with screen-width into my custom arguments like -w?
Thanks for explanation!

Can’t you just use the arguments provided by Unity? They’re just a little longer.

What I meant is, you could just write a batch-file (.bat) which takes the arguments and starts the actual game.(.exe). Same could be done with a console application that does exactly what you were trying in unity.
You could then start this one (either by click or via command line), which then parses the arguments, starts your game with the forwarded arguments and exits afterwards. That’d be kinda the minimal game launcher you could create lol.

Or just ask Unity to make command line arguments customizable :p.

No I mean How do I understand?

Unity Player with custom argument -w -h etc. with unity player’s argument replacements like shell with argument or process with arguments or what?

Hello everyone, nothing help me???

using System;
using UnityEditor;
using UnityEngine;

public class StartupScript {

    static string jWidth = ""; // unity player width #
    static string jHeight = ""; // unity player height #
    static string jWindowed = ""; // unity player windowed

    // Use this for initialization

    private static string GetArg(string name)
{
    var args = System.Environment.GetCommandLineArgs();
    for (int i = 0; i < args.Length; i++)
    {
        if (args[i] == name && args.Length > i + 1)
        {
            return args[i + 1];
        }
    }
    return null;
}

    static void PerformBuild () {
        // width
        jWidth = GetArg("-width");

        // height
        jHeight = GetArg("-height");

        jWindowed = GetArg("-windowed");

        Screen.SetResolution(int.Parse(jWidth), int.Parse(jHeight), false);
    }
   
    // Update is called once per frame
}

And I type command with unity player but it cannot parse current width and height?
How do I fix?

And It compiles and creates fresh executable
And I try call parameters but it doesn’t work?

nothing width and height :frowning:
Thanks!

You can’t do custom command line arguments and have it work before your code executes. If you want to use the command line to set width and height right from the time the app boots up then you have to use the args that the engine already understands.

Okay than I will create own Engine with OpenTK 2.0 via Gl.Shader() ( no using Gl.Begin() and Gl.End() ) Do you know SimpleScene Manager ( It looks very old with OpenTK 1.0 and it works fine ) But I wish I have experience about Resolution with fullscreen like sketched resolution if your monitor shows 800 x600 into sketched fullscreen.

PS: Why Unity Player can not customize? So sadly! I work back with OpenTK and BulletSharp ( It is like Collision Detection or Physics etc… )

It can. Your code works. The built player just has to get to the point in its lifecycle where it executes your code. You’ve stated that this level of functionality isn’t sufficient for your requirements. Otherwise, request a new feature and let people vote on it. https://feedback.unity3d.com/

Hello I argue that. Why they force me to get solutions? I think Unity always does shit. But I want my game project is finish. :frowning: Do you mean PlayerPrefs?

Sorry I miss understand that.

I tried but it doesn’t work why ???

using System;
using UnityEngine;

public class CommandLineArguments : MonoBehaviour {

    string[] args = Environment.GetCommandLineArgs();
    string mWidth = "";
    string mHeight = "";

    void Start()
    {
        for (int i = 0; i < args.Length; i++)
        {
            if (args[i] == "-width" || args[i] == "-w")
            {
                mWidth = args[i + 1];
            }

            if (args[i] == "-height" || args[i] == "-h")
            {
                mHeight = args[i + 1];
            }
        }
    }

    private void OnGUI()
    {
        PlayerPrefs.SetInt("Screenmanager Resolution Width", int.Parse(mWidth));
        PlayerPrefs.SetInt("Screenmanager Resolution Height", int.Parse(mHeight));
    }

    void OnApplicationQuit()
    {
        PlayerPrefs.SetInt("Screenmanager Resolution Width", 800);
        PlayerPrefs.SetInt("Screenmanager Resolution Height", 620);
        PlayerPrefs.SetInt("Screenmanager Is Fullscreen mode", 1);

        PlayerPrefs.Save();
    }
}

PS: Stop fake me! I cannot find example of google search.

What do I PlayerPrefs? I tried PlayerPrefs and PlayerPrefs doesn’t recognize to numbers from commandline arguments.

Nothing answer me??? Thanks!

The answers would just be the same as above.

Start does not run when the application starts up. You need to pass the arguments when you start the application, and like already mentioned, if you want to name the parameters differently, you’ll need an external start-up application / script / batch file.

If you don’t want that, you could kindly suggest this as an improvement for future versions. But I’m not sure whether they would want to allow the engine’s users to mess with the arguments names.

Alternatively, by the source code license. :stuck_out_tongue:

Because you’re setting the PlayerPref resolution to 800x600 when you quit. And really all this gets you is booting up the app with the last resolution that was used. If you changed your command line args in the interim then you’d still get the same behavior you’re seeing now because that’s how the engine works.

Again. This is supported. Look here Unity - Manual: Command-line arguments you use -screen-width and -screen-height.