Search Unity

Help Wanted Engine for Browser Multiplayer Game using WebGl

Discussion in 'Multiplayer' started by richardbendler, Nov 10, 2020.

  1. richardbendler

    richardbendler

    Joined:
    Nov 10, 2020
    Posts:
    1
    Hello everyone, im currently working on a multiplayer browser game, which i want to host on my linux server. After looking how to implement the multiplayer, i decided on the mirror API, but all the tutorials i could find show that the game will be hosted by a player himself. Is it possible with mirror to have the serverlogic run on my server after i export to WebGL? Otherwise what are some good free alternatives to develop a browser multiplayer game, which i can host on my server? I already decided against Photon bc of the 20 player limit and against UNet because its deprecated soon
     
  2. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    109
    I am working on similar setup for my current project.

    Long story, short -- yes, you can totally run a "headless" Unity process as a Mirror "HOST" on your Linux box. Once you don that, you can can build WebGL game "clients" which will access your Linux server / Mirror HOST.

    For your host / server you would just go into to your Unity build settings and do the following:
    * set target platform to "Linux" (assuming you have that installed on your build machine)
    * select "server build" (this removes the GUI from trying to pop up on your server)
    * click the "Build" button
    * create a folder for your Linux build
    * pick a name for your build binary
    * After the build completes, move that entire folder over to your Linux box
    * set up the binary to execute with
    chmod 755 ./your_game_name
    for the binary
    * run the build via a command which looks something like
    ./your_game_name


    Assuming that your game is setup like mine is and the game requires the HUD GUI to start the hosting process, then the next step is to write some code wrapped checks for
    #ifdef UNITY_SERVER
    or
    UNITY_CLIENT
    on your host's first "scene" to recognize that it is a server build and to automagically setup and start listening for connections as only a HOST.

    Full disclaimer, as of writing this, I haven't done the final step in this process for my project yet, so I might update later if I learn that it doesn't work as I expected. Also, I am not an expert on this process yet, but let me know if you have any other questions and I'll try to help where I can.

    (edit: replaced backtick code comments with ICODE blocks)
     
    Last edited: Nov 10, 2020
    luke-unity likes this.
  3. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    190
    Yuju_dev, Joe-Censored and BetaMark like this.
  4. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    109
    Verified that the
    #ifdef UNITY_SERVER
    works like I expected it to -- that code only fires if you have compiled with the "Server Build" checkbox (assuming you are in Unity 2020 -- I think it was called Headless or something similar in previous versions).

    So my new flow for my project will be:
    * create a new empty startup scene
    * place a new startup scene as scene 0 in the "scenes to build"
    * on a startup scene, have one game object with a single script which will check for UNITY_SERVER and fire up my Mirror HOST behaviors without waiting for user input
    * if this is not a UNITY_SERVER, then move onto the "main menu" scene (or whatever your game client would normally start with)

    This way I can compile for webgl clients and the dedicated server just by switching the target platform and check the "server build" in build settings. If I discover any other optimizations for this process, I'll send them your way.
     
    Yuju_dev and Joe-Censored like this.
  5. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    190
    I like to write small tools to create the builds. Makes it very easy to build the server for a different platform and run a different scene in each of them. Something like this:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. public static class BuildTool
    3. {
    4.     [MenuItem("Tools/Build/Build Client")]
    5.     public static void BuildClient()
    6.     {
    7.         var bpo = new BuildPlayerOptions
    8.         {
    9.             scenes = new[]{"Assets/Scenes/MainClient.unity"},
    10.             locationPathName = "Build/Client/",
    11.             options = BuildOptions.None,
    12.             target = BuildTarget.WebGL,
    13.         };
    14.         BuildPipeline.BuildPlayer(bpo);
    15.     }
    16.  
    17.     [MenuItem("Tools/Build/Build Server")]
    18.     public static void BuildServer()
    19.     {
    20.         var bpo = new BuildPlayerOptions
    21.         {
    22.             scenes = new[] { "Assets/Scenes/MainServer.unity" },
    23.             locationPathName = "Build/Server/MyServer.x86_64",
    24.             options = BuildOptions.EnableHeadlessMode,
    25.             target = BuildTarget.StandaloneLinux64,
    26.         };
    27.         BuildPipeline.BuildPlayer(bpo);
    28.     }
    29. }
    30. #endif
    This creates a Tools menu item which you can use to create builds. You could also add an option to build both with just one click by calling both BuildServer and BuildClient. (BuildOptions.EnableHeadlessMode is the same thing as ticking the Server Build box in the build settings)
     
    Last edited: Nov 11, 2020
    Andreas36, Joe-Censored and BetaMark like this.
  6. NoamSapir

    NoamSapir

    Joined:
    Jul 25, 2018
    Posts:
    38
    Mirror is now finally supports SSL for WebGL builds, highly recommending using it.
     
    Brodus, BetaMark and luke-unity like this.
unityunity