Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Host/Single Player mode for WebGL

Discussion in 'Netcode for GameObjects' started by DrViJ, Dec 26, 2022.

  1. DrViJ

    DrViJ

    Joined:
    Feb 9, 2013
    Posts:
    158
    I want to make a prototype with singleplayer/multiplayer modes. My idea was to simplify development by implementing single player mode as Host Mode of netcode for gameobjects with no players except host. But I have a problem.
    Unity Transport 2.0 is available for WebGL with Client only support cause of websockets, So host mode is not available there. What is the best practices to implement single player mode for multiplayer games on WebGL? should I have two separate codebases with and without netcode for gameobjects in such case?
     
    DungDajHjep likes this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,515
    Well, if you ask me, my best practice would be to drop support for WebGL and focus on the other platforms. ;)

    But yes, you should probably consider making a common interface class for all Netcode (host) functionality and only call into that. That way you can build for WebGL with the same codebase and only need to swap out the implementation of the interface class. But it could still be a lot of extra code just for a single platform to have a single-player mode experience. Is it really worth it, or required?
     
    DrViJ likes this.
  3. DrViJ

    DrViJ

    Joined:
    Feb 9, 2013
    Posts:
    158
    Yes, WebGL is one of my primary platforms :(
     
  4. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    427
    You could write a custom transport for your single player mode. Since client and host would be in the same process, you wouldn't even need to do any real networking. You could simply store the packets in static hash maps and lists. Then, all your code would remain the same for single and multiplayer and the only difference would be which transport you configure NGO with.

    The library on which the Unity Transport is based actually supports that very use case. It's just not exposed through NGO unfortunately (would make for a nice feature request). If you're willing to hack around in the Unity Transport codebase, what you'd want to do is add some kind of hook/setting to allow using
    IPCNetworkInterface
    instead of
    WebSocketNetworkInterface
    when creating the
    NetworkDriver
    .
     
    DungDajHjep and DrViJ like this.
  5. DrViJ

    DrViJ

    Joined:
    Feb 9, 2013
    Posts:
    158
    The NGO looks very flexible! Thank you so much for the advice! I will try!
     
    DungDajHjep likes this.
  6. DrViJ

    DrViJ

    Joined:
    Feb 9, 2013
    Posts:
    158
    I replaced WebSocketNetworkInterface with IPCNetworkInterface and removed exceptions throw for webgl, and it worked. Thank you so much for your help!
     
    simon-lemay-unity likes this.
  7. travisww

    travisww

    Joined:
    Feb 6, 2023
    Posts:
    1
    For anyone getting the error: "Exception: WebGL as a server is not supported by Unity Transport, outside the Editor." You can comment out the exception in that same script and it will run fine. Thanks for the solution!
     
    bugfinders likes this.
  8. PeterDrake

    PeterDrake

    Joined:
    May 22, 2018
    Posts:
    12
    1) In which file do I find this?

    2) How do I edit Unity's code? I can view it in my IDE (Jetbrains Rider), but I get a warning that "This file is part of the Unity Package Cache. Any changes made will be lost.".
     
  9. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,515
    You'd have to copy the package contents to the Assets tree and then uninstall the package from Package Manager. Then you can edit it, but you don't see whether there's a newer version. If you get a newer version, you'd have to completely delete your version of the package, copy the new package version to the previous location, then apply all changes again. Deleting is important because the new package may have renamed or deleted files that would then remain and cause compile errors if you don't entirely remove the existing package content.