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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

WebGL and UNET

Discussion in 'WebGL' started by knickerbocker, Oct 21, 2015.

  1. knickerbocker

    knickerbocker

    Joined:
    May 8, 2014
    Posts:
    29
    Hi there,

    Not really sure what's going on but i hear that UNET supports Web Sockets with WebGL. Is this true?

    I've tried using the Network Manager class and can't create a local or internet hosted game in my webgl game.

    "Multiplayer Gaming
    Use UNET in 5.1+ as this will work out of the box as it’s using the WebSocket API – you can also write JavaScript code to directly use WebSockets yourself."

    Got this from the following link:
    http://thecreativechris.com/tag/unet/

    Can anyone please elaborate on the process required to use Web sockets instead of UDP with my project.

    Is it a case of #IF UNITY_WEBGL ?

    Any help is appreciated.
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Yes!

    It's just a boolean property (useWebSockets) in UnityEngine.Networking.NetworkLobbyManager class (inherited from UnityEngine.Networking.NetworkManager)

    The Unite training day project on the Asset Store shows how to use UNET, and it works on WebGL.

    Here is how to try it:
    1. Create a new Project and Import the AS package
    2. Open LobbyScene
    3. In the LobbyManager game object => LobbyManager component => NetworkInfo, enable "Use WebSockets"
    4. Build and Run for WebGL
    5. Play LobbyScene in the Editor
    6. In the Editor, select Manual Connection => Play and Host
    7. In the Browser, select Manual Connection => Join a Game
    8. Play the game!
    Just make sure you use Unity 5.2.2
     
  3. knickerbocker

    knickerbocker

    Joined:
    May 8, 2014
    Posts:
    29
    Thanks for your reply.

    I can create an internet game in the editor but cant create or join in browser.

    i've got the files hosted on my website, and it says 'Offline' on the top right when i run it. Any buttons i press seem to hang.
     
  4. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    any error in the browser log ?
     
  5. knickerbocker

    knickerbocker

    Joined:
    May 8, 2014
    Posts:
    29
    MatchMakingClient Create :https://mm.unet.unity3d.com/json/reply/CreateMatchRequest

    tankgl.js:1:193567
    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://mm.unet.unity3d.com/json/reply/CreateMatchRequest. (Reason: CORS header 'Access-Control-Allow-Origin' missing). <unknown>
    Request error: Unknown error.

    tankgl.js:1:193495

    Raw response:

    tankgl.js:1:193495

    Create Failed:[[UnityEngine.Networking.Match.CreateMatchResponse]-success:False-extendedInfo:]-address:,port:0,networkId:0x0000000000000000,nodeId:0x0000,usingRelay:False

    tankgl.js:1:193495


    Yep i get this message in the log" The same origin policy disallows reading the remote resource at.."
     
  6. nastasache

    nastasache

    Joined:
    Jan 2, 2012
    Posts:
    74
    I've tried also, it's a great resource to use UNET both in desktop and browsers multiplayer app, but WebGL results was:

    1) "An error occured running the Unity content on this page. See your browser's JavaScript console for more info. The error was: SyntaxError: return not in function"

    Solved by using Optimization Level "Fastest (very slow build)"
    (this is #1 global issue when compile for WebGL, default optimization level never works except for "Hello World" like games - well, what about to made a complex FPS multiplayer?)

    Then:

    2) "The browser could not allocate enough memory for the WebGL content. If you are the developer of this content, try allocating less memory to your WebGL build in the WebGL player settings"

    Solved by increasing memory from 256 to 2048.
    (this is #2 global issue when compile for WebGL, default memory allocation level never works except for "Hello World" like games - well, again, what about to create a FPS multiplayer?). How many people have 2GB ram to allocate for?

    Then:
    3) "An error occured running the Unity content on this page. See your browser's JavaScript console for more info. The error was: RangeError: invalid array length"

    Solved? None. I have no time to try more. Sorry.

    I think, if a simple project have 80MB JS file to be loaded&executed on the client side, I have not too much expectations from WebGL builds.

    Lot of time allocating on reducing project complexity instead focusing on game logic.

    Sorry for criticism, is not about UTDP & websockets or UNET, but WebGL solution really looks bad in Unity, in my opinion.
     
  7. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    We can take a look at it, but first, please confirm which version of Unity you are using.

    "try allocating less memory" and "Solved by increasing memory from 256 to 2048"

    that doers not make sense to me.

    80mb sounds a lot to me
    • Have you enabled stripping?
    • Have you uploaded the release build ?
    • Have you configured your server to "serve" the compressed gz files (if you don't know how to do that try 5.3 beta which will always used compressed files)
    • What's the size of your Compressed folder ?
     
    knickerbocker likes this.
  8. tushar_unity775

    tushar_unity775

    Joined:
    Jun 17, 2022
    Posts:
    20
    is unet suppoted in unity 3d webgl for multiplayer networking