Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Unity Multiplayer Mirror - Networking for Unity (UNET Replacement)

Discussion in 'Connected Games' started by vis2k, Aug 11, 2016.

  1. VTaredi

    VTaredi

    Joined:
    May 16, 2017
    Posts:
    2
  2. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    39
    Thanks!! I'll try the WebRTC method. Sounds like the way out here!

    FYI I did try dissonance with LLAPI and Mirror but got some random Dissonance "out of resources" errors and I just assumed it may be due to reworked code or something Idk. The Networking DLL's were replaced with Unet Community before mirror. Hard to know where I'm at now!
     
    Nyarlathothep likes this.
  3. NFMynster

    NFMynster

    Joined:
    Jul 1, 2013
    Posts:
    42
    Hello @vis2k
    I'm a bit confused whether or not it's based off UNET, as you mention in the description it's based on Telepathy?
    Thanks for making this though, awesome.
     
  4. PcJax

    PcJax

    Joined:
    Feb 21, 2018
    Posts:
    2
    I'm okay with mirror being tcp by default, but not having access to multiple channels with different reliability on the hlapi even if we can change the transport to udp changes a lot of things.

    Currently I'm using https://partner.steamgames.com/doc/api/ISteamNetworking#SendP2PPacket to map UNet channels to Steam Networking, sending my voice chat on k_EP2PSendUnreliableNoDelay, that works well testing with UNet default transport or changing it to Steam. With mirror I have to send eveything as k_EP2PSendReliable leaving out my voice chat outside from the already established communication.
     
  5. PcJax

    PcJax

    Joined:
    Feb 21, 2018
    Posts:
    2
    NetworkManager.activeTransport Was added on 2018.3 so at least this line doesn't work:
    NetworkManager.activeTransport = new EnetUnetTransport();
     
  6. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    You can use any backend that you want by setting Mirror.Transport.layer to new LLAPITransport() etc. Look at the source code to see how to make a 'TransportLayer' class.

    Telepathy is the low level networking.
    Mirror is a fork of the original UNET high level networking.
     
  7. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    677
    Another really good feature just got merged into mirror. Starting with release 391 there are massive gains in bandwidth when using SyncLists.

    Previously, any change to a synclist triggered a message. Suppose you changed 10 items in a list, it would immediately send 10 messages to the clients. This means roughly 600 bytes in TCP/IP headers. Typical workload near an NPC while you trade and craft.

    With this change, synclists are now synchronized as part of OnSerialize/OnDeserialize. If you change 10 items, the synclist respects your SyncInterval settings. The changes all go together in one message, reducing the overhead to 0-60 bytes in TCP/IP headers.

    The change greatly simplify the code. A whooping 320 lines of weaver witchcraft were removed in the process. Test coverage nearly doubled, and it even fixes one long standing bug with synclists where you could not add the same component (or derived components) twice to a gameobject if it had a synclist. The change has been tested in ummorpg, usurvival and Cubica.

    Thanks to vis2k for looking over my shoulder and keeping me honest while I essentially rewrite synclists.
     
    Last edited: Oct 13, 2018
    Jos-Yule and vis2k like this.
  8. Vincenzo

    Vincenzo

    Joined:
    Feb 29, 2012
    Posts:
    45
    I really like your work Goldbug, but I have to correct you one one point, if you would keep LLAPI underneath as the transport layer, it would have added those messages together automatically and send it in one packet not having such IP header overhead. regardless good job, I suppose it makes more sense cause you have your own TCP transport layer underneath that is not doing such things.
     
  9. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    39
    How's that Transport Sync script coming along? You find it?
     
  10. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    We will announce it when it's done
     
    Roamer79 likes this.
  11. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    hungrybelome, goldbug and Roamer79 like this.
  12. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    677
    That is possible, everything I said applies to Telepathy. LLAPI is completely unusable for my project, so I did not even bother testing this with LLAPI.

    That said, whether you used LLAPI or Telepathy, any change to a list was sent immediately. Your SyncInterval settings were ignored. Now you control how much batching happens by adjusting your SyncInterval.

    Have you validated the batching with LLAPI? you can check with wireshark.
     
    Last edited: Oct 17, 2018
  13. bug5532

    bug5532

    Joined:
    Aug 16, 2011
    Posts:
    291
    So I think I must have done something wrong...
    I get this error when I run in a build, seems to be working in editor,

    Code (CSharp):
    1. TypeLoadException: Could not load type 'System.IO.InvalidDataException' from assembly 'Mirror.Runtime'.
    2.   at Mirror.Protocol.UnpackMessage (System.Byte[] message, System.UInt16& msgType, System.Byte[]& content) [0x00008] in C:\projects\hlapi-community-edition\Mirror\Runtime\UNetwork.cs:114
    3.   at Mirror.NetworkConnection.HandleBytes (System.Byte[] buffer) [0x00001] in C:\projects\hlapi-community-edition\Mirror\Runtime\NetworkConnection.cs:206
    4.   at Mirror.NetworkConnection.TransportReceive (System.Byte[] bytes) [0x00001] in C:\projects\hlapi-community-edition\Mirror\Runtime\NetworkConnection.cs:269
    5.   at Mirror.NetworkClient.Update () [0x0010f] in C:\projects\hlapi-community-edition\Mirror\Runtime\NetworkClient.cs:234
    6.   at Mirror.NetworkClient.UpdateClients () [0x00030] in C:\projects\hlapi-community-edition\Mirror\Runtime\NetworkClient.cs:349
    7.   at Mirror.NetworkIdentity.UNetStaticUpdate () [0x00007] in C:\projects\hlapi-community-edition\Mirror\Runtime\NetworkIdentity.cs:1011
    8.   at Mirror.NetworkManager.LateUpdate () [0x00001] in C:\projects\hlapi-community-edition\Mirror\Runtime\NetworkManager.cs:162
    9. (Filename: C:/projects/hlapi-community-edition/Mirror/Runtime/UNetwork.cs Line: 114)
     
  14. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    677
    @bug5532 can you get on Discord so that we can troubleshoot this issue?

    Open the player settings, are you using .net subset for api compatibility? try switching to .net instead.
     
    vis2k likes this.
  15. bug5532

    bug5532

    Joined:
    Aug 16, 2011
    Posts:
    291
    I fixed it using experimental .net 4.6 last night! Just did another build with stable .net 3.5 and changing to non subset, that also fixed it :)
    Thanks Goldbug!
     
    vis2k likes this.
  16. validname1

    validname1

    Joined:
    Feb 1, 2018
    Posts:
    23
    Converted my project today from unet to mirror, unfortunately my synclistint broke as it says "synclist not initialized". What am I doing wrong here?
    Code (CSharp):
    1. public SyncListInt network_Equipment = new SyncListInt();
    2. public SyncListInt network_Bodyparts = new SyncListInt();
    3.  
    4.  
    5.     public override void OnStartServer()
    6.     {
    7.  
    8.         c = GameObject.Find("Root").GetComponent<core>();
    9.         c.addPlayerToList(this.gameObject);
    10.  
    11.         curNode = c.mapHandler.GetMap(curMapID).GetNode(curNodeID);
    12.         Teleport(new Vector3(curNode.transform.position.x, curNode.transform.position.y, 0));
    13.  
    14.         inventory = new s_inventory(c, this);
    15.         for (int i = 0; i < inventory.s_InventoryEquipment.Length; i++)
    16.             network_Equipment.Add(Constants_Zeta.blankID);
    17.  
    18.         for (int i = 0; i < (int)Constants_Zeta.Bodyparts.MAX; i++)
    19.             network_Bodyparts.Add(Constants_Zeta.DefaultBodyParts[i]);
    20.  
    21.         InvokeRepeating("SavePlayer", 10f, 10f);
    22.  
    23.         base.OnStartServer();
    24.     }
    This is being ran on my player prefab. It errors specifically at the .Add functions. Running as a host.
     
    Last edited: Oct 24, 2018
  17. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    And it works in default UNET? That's strange.
    You could try doing it in Start() with a 'if isServer' check. But if it works in default UNET and not in Mirror then please open a bug report on our github page (see first post) and include a sample project.
     
  18. electricfalconllc

    electricfalconllc

    Joined:
    Oct 25, 2018
    Posts:
    1
    I just want to say thank you for making this. Its been very helpful to me!
     
    vis2k likes this.
  19. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    We are glad that you like it!
     
  20. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    62
    With the recent news regarding the new transport layer and the fps sample released by Unity I'm considering looking into Mirror as an alternative as I'm currently using Unet HLAPI.

    Is there any plans to support Mirror with future versions of unity? 2019.1 and beyond? Specifically when UNET is official removed from future builds?

    I know there has been a lot of questions in this thread regarding UDP vs TCP. Considering Unet is UDP, perhaps it's worth adding something to the original post highlighting UDP functionality with Mirror?
     
    vis2k likes this.
  21. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    677
    The plan is to support LTS versions of unity, that means 2018.4 when it is released

    That said Mirror should work with 2018.x versions, if you find a problem, feel free to open an issue or even a pull request.

    You can change the transport to UDP by adding this to your network manager:

    Code (CSharp):
    1. public override void InitializeTransport()
    2. {
    3.     Transport.layer = new LLAPITransport();
    4. }
    But honestly I would not trust LLAPI to babysit my pet rock.
     
    Last edited: Oct 28, 2018
    Brian-Crandell and vis2k like this.
  22. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,347
    Anyway to use both TCP and UDP for different messages so they are handled differently? instead of just only having either TCP or UDP?
     
  23. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    677
    Not at this point. I have a WIP pull request for adding channels which could theoretically be used for this, for example, the reliable channel would go through TCP and the unreliable channel would go through UDP.

    Someone has to write a transport that multiplexes between TCP and UDP. It would not be too difficult, I reckon 300-500 LOC. It should probably be an addon.
     
  24. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    13
    want use NGUI to replace networkmanagerHUD but don't nknow why i can't. how must call functions inside networkmanager like in networkmanagerHUD? when i want rewrite networkmanagerHUDwith NGUI all seems well but give me an error like this ClientScene not contain localplayer
     
  25. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    677
    @gamer2300 Mirror makes no assumptions about what kind of UI you use.

    NetworkManagerHUD is provided just so that it is easy to get started, but I don't think anybody will actually use it for their own game.

    You will still have a NetworkManager, but you just create a component that looks like NetworkManagerHUD with whatever UI you want. Copy the source for NetworkManagerHUD, and just replace all the UI stuff with your own UI.
     
  26. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    13
     
  27. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    13
    yes goldbug have tried but i see CLIENTSCENE don't have local player..in practice all code seems works but in visual studio have an error on ClientScene.localplayer and ClientScene.addplayer, for this two it say there is not defintion inside clientscene. Why this i have copied well all code..help me if you can thanks
     
  28. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    I think we don't really have a choice here by the way. @goldbug needs it for Cubica. I need it for uMMORPG and my other assets. Unity's new networking is too complicated for both of our small brains, so we will support Mirror no matter what.
     
  29. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    Please post the full error message and your code that calls it.
     
  30. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,347
    This would be the perfect hybrid approach :)
     
  31. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    677
    Yes, it would be a good approach IMO, but someone other than me will have to write that transport. I have an actual game to write and this is not an issue for me. We need someone who actually needs this.

    That said having channels makes this possible, so it is a good stepping stone.
     
  32. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,347
    I think it would be great for uSurvival for the firstperson aspect personally
     
  33. validname1

    validname1

    Joined:
    Feb 1, 2018
    Posts:
    23
    I've figured it out. So during the transition from UNET to Mirror, the new network manager referenced a deactivated test gameobject I had on my scene called "Player" instead of the actual prefab called "Player" inside my resources folder. Hopefully this helps others out.
     
    vis2k likes this.
  34. validname1

    validname1

    Joined:
    Feb 1, 2018
    Posts:
    23
    Ran into another issue, unfortunately this isn't as straightforward, perhaps you may have an idea. So for my game I am launching it locally using a custom NetworkManager and calling StartHost, which has not been overridden. The problem is that I'm getting tremendous fps lag from the profiler if I don't define a "LateUpdate" function, weird already, but whatever.

    So I define a "LateUpdate" function inside my NetworkManager like so and leave it blank:
    Code (CSharp):
    1. void LateUpdate()
    2. {
    3. }
    Good news is that framerate is substantially better. Bad news is that StartHost is now broken and doesn't spawn anything for the client. No local player, local gameobjects, nada! There are no error messages whatsoever as well. Oddly enough all the server-only stuff works just fine, so the "world" the game is running on is still there, just not the client copy.

    Any idea why NetworkManager would refuse to spawn the client player unless I remove LateUpdate? This was not happening on normal UNET.

    If it helps, here is a stripped down version of my NetworkManager

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5. using System.IO;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using ItemSystem;
    8. using System;
    9.  
    10. public class Server : NetworkManager
    11. {
    12.     core c;
    13.  
    14.     // Use this for initialization
    15.     public void Start()
    16.     {
    17.     }
    18.  
    19.     //Detect when a client disconnects from the Server
    20.     public override void OnServerDisconnect(NetworkConnection connection)
    21.     {
    22.         p_data p = connection.playerControllers[0].gameObject.GetComponent<p_core>().data;
    23.         UnloadPlayer(p);
    24.         Debug.Log(p.name + " disconnects.");
    25.         base.OnServerDisconnect(connection);
    26.     }
    27.  
    28.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    29.     {
    30.         var player = (GameObject)GameObject.Instantiate(playerPrefab);
    31.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    32.     }
    33.  
    34.  
    35.     // Use this for initialization
    36.     public override void OnStartServer ()
    37.     {
    38.         c = GameObject.Find("Root").transform.GetComponent<core>();
    39.         c.isServer = true;
    40.         c.mapHandler.Intialize_Server();
    41.         LoadPlayers();
    42.     }
    43.  
    44.     bool init = false;
    45.     void Update () {
    46.         if (NetworkServer.active && init == false)
    47.         {
    48.             init = true;
    49.             c.mapHandler.Initialize_Server_Post();
    50.         }
    51.     }
    52.  
    53.     void LateUpdate()
    54.     {
    55.     }
    56.  
    57. }
    58.  
    This is what the profiler spews out if LateUpdate is commented out

    What my NetworkManager looks like


    Yeah.... I just don't know what's going on anymore. It's either I remove "LateUpdate" and play with tremendous fps lag or I keep LateUpdate and nothing spawns for the client side of "StartHost" anymore. And lastly, yes, the lag is still there if I launch my game in server-only mode with "LateUpdate" removed, build, etc etc.
     
    Last edited: Oct 30, 2018
  35. bug5532

    bug5532

    Joined:
    Aug 16, 2011
    Posts:
    291
    By adding a LateUpdate function you are hiding the LateUpdate function in the inherited networkmanager, which basically calls all the network code:
    Code (CSharp):
    1.    // NetworkIdentity.UNetStaticUpdate is called from UnityEngine while LLAPI network is active.
    2.         // if we want TCP then we need to call it manually. probably best from NetworkManager, although this means
    3.         // that we can't use NetworkServer/NetworkClient without a NetworkManager invoking Update anymore.
    4.         //
    5.         // protected so that inheriting classes' LateUpdate() can call base.LateUpdate() too
    6.         protected void LateUpdate()
    7.         {
    8.             // call it while the NetworkManager exists.
    9.             // -> we don't only call while Client/Server.Connected, because then we would stop if disconnected and the
    10.             //    NetworkClient wouldn't receive the last Disconnect event, result in all kinds of issues
    11.             NetworkIdentity.UNetStaticUpdate();
    12.         }




     
    validname1 likes this.
  36. yusufdemir2012

    yusufdemir2012

    Joined:
    Dec 21, 2013
    Posts:
    12
    Hey! I am trying to build release from source code with visual studio. The compiles are succeed but I get error when I copy dlls to unity project. After copy dlls, Unity is not combiling project and not finding "Mirror" namespace. Can you help me?
     
  37. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    Use the Asset Store project
     
  38. yusufdemir2012

    yusufdemir2012

    Joined:
    Dec 21, 2013
    Posts:
    12
    But I need to make a few changes in network code(NetmorkManager.cs). Even if I had not make any changes on the source code, unity fails when copy dlls to unity project. Can you help me?
     
  39. Lleot

    Lleot

    Joined:
    Mar 27, 2014
    Posts:
    2
    Hi
    Thanks a ton for this project! I managed to work out how to use it in the end but now I'm faced with a weird problem where I am using the network animator but sometimes my animation gets lost? In that it plays on the host and doesn't play on the client, or that it plays on the client locally but doesn't play on the host. Is there any tips you can give as to diagnosing why this might be happening?
     
  40. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    Check out the tutorial on the github readme page.

    NetworkAnimator is still the default one from UNET, can't recommend it.

    What I do in uMMORPG is synchronize the player's state (IDLE/MOVING/DEAD/etc.) and the movement position etc. and use that on the client for animations. I am not sure if we should even have a NetworkAnimator component in Mirror. You synchronize all the state anyway, so what's the point in synchronizing the animator too.
     
  41. fran_molina

    fran_molina

    Joined:
    Aug 23, 2017
    Posts:
    1
    Hello people!

    Really nice work you are doing here! I have read here that there were some issues with running Mirror on UWP and HoloLens. Has somebody here managed to do that? I am getting some compile errors on Visual Studio.
     
  42. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    Never tried UWP myself, but I think someone mentioned that Unity deprecates support for it anyway, so we didn't worry about it?
     
  43. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,112
    vis2k likes this.
  44. bug5532

    bug5532

    Joined:
    Aug 16, 2011
    Posts:
    291
    Have you removed support for multiple local players? there doesn't seem to be any playercontrollerID code in the addplayer section anymore...
     
  45. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    Thanks

    What are the errors that you are getting?
    I reopened the issue: https://github.com/vis2k/Mirror/issues/43 . Can you go there and provide as much info as possible?
    I honestly have no clue what UWP really is or why anyone needs it or if we should or should not support it, or if Unity will or will not support it in the future. Any info helps.

    Yes we have. Didn't seem to be worth supporting for the rare games that are both multiplayer and local splitscreen.
     
    Last edited: Nov 7, 2018
  46. MCoburn

    MCoburn

    Joined:
    Feb 27, 2014
    Posts:
    67
    Ignorance reliable UDP Transport for Mirror

    Just thought for all those people who need or want UDP transport for Mirror and don't want to use Unity Tech's old, outdated and poorly performing LLAPI that you should try my Ignorance Reliable UDP Transport located here on my Github. Ignorance uses the power of a tried and true ENET library and the ENET CSharp wrapper by nxrighthere.

    At the time of writing, Ignorance is at version 1.0.1 and already has both server and client functionality working, complete with reliable and unreliable channels.

    While this transport is NOT official, direct all Ignorance Transport questions to me here (in this topic, I don't do private message support) or find me on the Mirror discord. If I'm not busy or hanging out with the developers, I'll do my best to help solve your issue.

    Just follow the instructions in the readme and you'll have a UDP data hose ready to spray data all over your server. Enjoy.
     
    nirvanajie likes this.
  47. blacksun666

    blacksun666

    Joined:
    Dec 17, 2015
    Posts:
    143
    UWP=Universal Windows Platform, ie games/apps for windows (includes the XBOX games console) that are for sale on the Microsoft store. I would hope this is a platform you want/need to support.
     
  48. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,347

    That's cool, though this implementation doesn't use a channel system it's still either mirror using TCP or UDP?

    It would be great to see a channel system where you can just use an unreliable UDP channel for movement, and everything else TCP, but I'm too noob and busy to attempt an implementation, would love to see an example of it though.
     
  49. MCoburn

    MCoburn

    Joined:
    Feb 27, 2014
    Posts:
    67
    I'm not 100% sure how to answer your question, but we have been considering a multi-transport option that allows one to spawn a TCP Transport as well as a UDP transport. Ultimately it brings the question, why not just use one transport or the other, as you have extra overheads and processing to deal with.

    Channels have been reimplemented for Mirror in recent commits so if you use this transport and use channel 0, ENET will send data via reliable UDP. Channel 1 is unreliable sequenced UDP. Telepathy IIRC doesn't care about channels because TCP is always reliable mode; Ignorance simply maps what reliable/unreliable channels you have into the appropriate packet flags and then sends it off to a mailing pigeon.
     
  50. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,141
    We added channels again a few days ago. You can call the Send function with a channel parameter.