Running WebGL with arguments on startup from browser

Hi Unity Forum, I am new here so in case this is the wrong place to ask a question like this, please bear with me.

I am working on a Unity application that is meant to be built into a WebGL application and will run embedded in a browser window.

That, in itself, is not a problem, but I need the same program to receive/fetch some information specifying which animations that should be played inside the application when it is run.

As an example, let’s say that I have 2 buttons on my internet page, and 2 animations in my Unity application. Animation 1 should be played if button 1 is pressed and vice versa. Is there any way to achieve this?

I have searched around for an answer, but i can’t seem to find the solution. In the Unity documentation I found this: “Unity - Manual: Interaction with browser scripting”, but this seems to be the Unity WebGL application calling the browser but what i want is the opposite - i.e. the browser calling the application.

Additionally, I found another solution that could be the function: “System.Environment.GetCommandLineArgs();”, However, in that case would I need to launch, my WebGL application in a way that allows me to pass it arguments, which according to my findings would be a command prompt of some kind. I don’t know how to do this from a browser or what the command should look like etc. so I don’t know exactly how this would work or how feasible it is.

Any solutions/suggestions as for how to achieve launching my WebGL application with such information would be highly appreciated.

Cheers!

This is also mentioned on the page, see the comment about SendMessage.

I have not tried this, but it should be possible to supply “Command line arguments” to your webgl content by adding something like

arguments: "arg1 arg2 arg3",

to the var Module = { block in your html.

Another option might be to use url arguments (ie http://www.myserver.com/mygame/index.html?arg1=bla&arg2=bla), and to query that string using Application.absoluteURL.

Thank you for the help!

After discussing and trying things out, it has been decided that the app are going to use your last suggestion(passing information through the URL and reading application.absoluteURL).

Do you know if hiding the URL from the user so as to prevent them from tampering with the URL parameters will alter the absoluteURL?

And as well as in the case of the app being embedded onto the website, will the absoluteURL remain the same?

Jonas, Im facing a similar problem. Im calling SendMessage from javascript and it’s not doing anything

SendMessage (‘MyGameObject’, ‘MyFunction’, ‘foobar’);

What is “MyGameObject” exactly? I have a class with that name instantiated in the scene, with a public MyFunction(value);

“MyGameObject” is a game object in your scene named “MyGameObject”.

Thanks Jonas

Hello Jonas.
I need to launch WebGL client with specified params. I mean params should be available just after splash screen/loading bar screen.
Can you help?

Assuming you would pass the arguments from JS, couldn’t you write a plugin?

Anyway, you can use Application.absoluteURL as mentioned by Jonas.

1 Like

Ok, thank you!

Hi,

I’d like to pass arguments to my WebGL unity app, as if it was launched as a standalone app via cmd with line args.

@jonas-echterhoff_1 You were talking about injecting args in the unityInstance.Module.

I tried to inject them in the UnityProgress function as following :

var argsGiven = false;
function UnityProgress(unityInstance, progress) {
  if (!unityInstance.Module)
    return;

  if (unityInstance.Module.arguments && !argsGiven) {
    console.log("ARGUMENTS")
    unityInstance.Module.arguments.push("myarg1");
    unityInstance.Module.arguments.push("myarg2");
    argsGiven = true;
  }
...

This correctly injects the arguments in the unityInstance.Module.arguments, but my app doesn’t detect them.
Do I need to do something more ?

EDIT:

Finally i decided to get it done via the url as suggested.
Just took the string[ ] arguments as follow :
(In my case I want to add each arg name and its value in a string[ ])

    private static string[] GetArguments()
    {
#if (UNITY_WEBGL || UNITY_ANDROID) && !UNITY_EDITOR

        // url with parameters syntax : http://example.com?arg1=value1&arg2=value2
        string parameters = Application.absoluteURL.Substring(Application.absoluteURL.IndexOf("?")+1);
        return parameters.Split(new char[] { '&', '=' });
#else
        return Environment.GetCommandLineArgs();
#endif
    }
    string[] arguments = GetArguments();
    // Do what you need to do with these arguments [arg1,value1,arg2,value2]
2 Likes

That looks like a nice solution to get the query args. One note is that web query args do not necessarily need to come with a value, so if one passed Example Domain , the above code would get confused, and return a string list [arg1,arg2,value], making it hard to distinguish what was key and what was value.

You’re right, I didn’t thought about these arguments.
It may be easier to add it in a Dictionnary<string, string> with (arg, value) pairs then, and (emptyArg, null) for args that do not require a value.