Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question how to make changes to variable in ready webGL build?

Discussion in 'WebGL' started by vemmu, Oct 20, 2022.

  1. vemmu

    vemmu

    Joined:
    May 15, 2022
    Posts:
    8
    I am building a web app arcade with express.js. For the games I use unitys webGL option. I am planning on to make about 5 games and I have a leaderboard on the serverside. So I use UnityWebRequests to send the data to the server. The problem I have is that I am only in the development phase, so I need to do the webrequests to http://localhost:8000/. But when I am done with the development I would need to change all the url's to "https://mydomain.com/". This would be so much extra work, so I was wondering is there a way to make either enviroment variables that I can just change when I need to, or if there is a third party site where I can easily redirect to certain domain with all the parameters and routes?

    Thanks in advance!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    You can make that work with preprocessor macros, for example DEVELOPMENT_BUILD. But that would mean that any non-development build will use the public address - that could be an issue testing release builds.

    You could also add a secret key combo that enables you to switch to the localhost address at any time. For the end user this will at best lead to a broken game which is fixed by restarting it. During development I'd prefer that option.

    You could also tie this in based on which domain/hostname the build is running. I'm sure there must be a way for the build to find this out.

    Lastly and probably the safest option is to use Javascript to provide the address. That way you can change it at any time by changing a simple javascript method. See: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

    Note that the default should be to use the public address because you may not be able to customize javascript for every deployment target, such as itch.io.

    Alternatively, make a web request to a server you own and within that request, tell the script from where or which version the request is coming from to identify the source of the request, and then provide it with an address (make sure it also works in case the request times out).
     
    vemmu likes this.
  3. vemmu

    vemmu

    Joined:
    May 15, 2022
    Posts:
    8
    Thank you, this helps alot. I think I am going to use the jslib method. So when the game starts I am going to call function on the browser side from unity that checks the current url, then it is going to use the SendMessage function to send the url to unity and finally unity is going save the url to playerPrefs. Sounds good!