Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Unity Multiplayer Mirror - Networking for Unity (UNET Replacement)

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

  1. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,383
    "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."

    Well I don't know about the technicalities of it, all I know is TCP better for reliable with delays and higher ping as the cost factor, and UDP better with unreliable sequenced where I don't care if a bunch of packets get missed out time to time, I'll just do something with the next packet, where the cost is well that is cost not caring about missed packets, the benefit being ping times and responsiveness just works a lot better without the delays inherit in using TCP.

    Which for my game I see advantages to using both so the hybrid approach of two different network transport connections kinda makes sense and I build things around utilizing UDP for fast moving data, and TCP for everything else that matters... I just know that not everyone has a super great connection (even minecraft was damn awful with any amount of packet loss, or ping to server that was in another country because it did use TCP for everything) and more people are wanting to play games with friends who live on the other side of the world.. and frankly TCP just sucks for doing that where ping time matters, but at the same time TCP is still useful in other areas.
     
  2. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,383
    But the transport is either TCP or UDP or is it reliable channel using TCP and unreliable using UDP?
     
  3. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    You can use any transport that you like (see Transport.cs class).
    Default is TCP.
    You could use a different transport for each channel if you wanted to..
     
  4. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,383
    how? Could it be something you might do for uSurvival like for the movement stuff anyway?
     
  5. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Check out the stuff in Mirror's transport folder.
    I won't do that for uSurvival, personally I think using two different transports is way too complicated.
     
  6. FizzCube

    FizzCube

    Joined:
    Feb 8, 2014
    Posts:
    4
    nirvanajie, vis2k and Player7 like this.
  7. Greyeeh

    Greyeeh

    Joined:
    Dec 10, 2014
    Posts:
    13
    Hey,
    I started using Mirror recently and I am having a small problem with players spawning.

    I'm adding the player with this method called on client:

    Code (CSharp):
    1. ClientScene.AddPlayer(0);
    Which results in this method being triggered on server:

    Code (CSharp):
    1. public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    2. {
    3.       GameObject player = Instantiate(playerPrefab);
    4.       Vector3 pos = Vector3.zero;
    5.  
    6.       player.transform.position = pos;
    7.  
    8.       NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    9. }
    Which works exactly as expected - new client is spawned across all existing clients.
    The problem is that this new client can't see any existing players that joined before he did.
    It was working in UNet few months ago so I figured it should work in Mirror too but for some reasons it ends up like this.
    Could you help me out with this? Do I have to manually sync old clients?
     
  8. yian-dev

    yian-dev

    Joined:
    Jun 26, 2017
    Posts:
    20
    Hey guys ive been looking in this thread but im confused.
    Post says this allows client-server code to be as one and thats so frustrating.
    Can i run a dedicated server that was build separately of the game project without rendering? thats what an mmo uses(alot more complex ofc) and i dont understand this project or the old Unity UNET, it seems in both this and old UNET the host (server) is the player? i keep searching for proper networking solution and i cant find anything.
    How can i make a game with dedicated server where collisions/transforms and everything that prevents cheating is run on a dedicated server? can this project Mirror achieve that?
    From the downloaded examples it seems like the server is the client, which is exactly what mmo's dont use or need, sorry i dont understand.

    Is there any tutorial on how to use this properly for beginners outside the examples? obviously im not going to learn using UNET from unity.
     
  9. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,875
    You can run Mirror / UNet as dedicated server in headless mode.

    Simply designing "host" as server only would make it a server. Host is already a server, simply do not execute any client logic on it.

    UNet is a bust right now. LLAPI is maybe somewhat competitive, if you're experienced enough, HLAPI is trash. Mirror (previously HLAPI Pro) fixed that one.

    Any previous UNet tutorial should be applicable to this one (Just make sure to find some API changes from manual if you encounter anything not working as intended).
     
    rubensollie and yian-dev like this.
  10. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Are you using the latest version? We removed the playerControllerId parameter a while ago.
    Also check out the Mirror demos on the Asset Store (free).
     
  11. yian-dev

    yian-dev

    Joined:
    Jun 26, 2017
    Posts:
    20
    Hi, what resources should i use for learning unet or mirror? im strictly interested in making server and clients separated, authoritative model and dont know where to go to, all tutorials i find about unet are listen servers wtf.
    Also the network manager component in Mirror demos spawns some UI to control the server but if i run in headless mode i cant even start the server?
    What are some resources to learn from im so lost...
    Are thes good tutorials to follow or they are not relevant to Mirror?
    https://unity3d.com/learn/tutorials/s/multiplayer-networking

    I also want basics stuff like client connects to server and server connects to databses and stores/loads data and updates the player, and because of the model of unet/mirror i dont understand at all how to separate server code from client it just doesnt make sense.
    How do i separate login servers, chat servers, and multiple servers if needed, is this even possible with Mirror in its current state or it needs to be hacked around to do these basics?
     
  12. jinnindo

    jinnindo

    Joined:
    Dec 8, 2015
    Posts:
    16
    I looked, but I'm not finding this, so even if it's something obvious, I'd appreciate a nice answer.

    There is no mirror-default way to connect non-lan players; we'd have to come up with our own punchthrough or server hosting and whatnot, is that right?

    If so, what's recommended?
     
  13. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Yes, go through the tutorials that you linked. Should still be 95% compatible.
    Also check out the Mirror project on the Asset Store, there are a few examples.

    I recommending renting a dedicated server or VPS and host your server there. This way everyone will be able to connect easily.
     
    jinnindo likes this.
  14. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    52
    I just downloaded the latest version and it looks like the localPlayers collection is no longer accessible from ClientScene. All references to ClientScene.localPlayers return an error saying "ClientScene does not contain a definition for localPlayers." How do I access this collection now?
     
  15. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    52
    Another question. What is the default connection timeout? Is this in a place I can read from code?
     
  16. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    There is only ClientScene.localPlayer now.

    Depends on the transport that you use.
     
  17. wdjones

    wdjones

    Joined:
    May 22, 2013
    Posts:
    1
    I'm having problems with this detecting steam running. It is getting detected inside unity or if I do a "Build and Run" inside unity. If I run the exe manually or run it through steam as a "non-steam game," steam is not being detected. This is my first go at dealing with connections through steam FYI.
     
  18. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    39
    Sorry I'm retarded. Can you please point to the exact example of how we use channels now or some documentation or a real quick example? Is it the same way as the orig UNET where we can designate the channel in the Command param?
    Thx
     
  19. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Send functions have a channel parameter and the Transport may or may not ignore it. E.g. Telepath (TCP) ignores it. UDP transports don't ignore it.
    Come by our discord if you can't get an example running!
     
  20. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    52
    I have a change request - feel free to ignore if you don't think it's useful. Would you consider making the following 3 NetworkManager static variables 'protected' instead of private? It would make overriding ServerChangeScene much easier - and would keep me from having to recompile from source every new build! :).
    s_StartPositions
    s_StartPositionIndex
    s_LoadingSceneAsync

    Thanks
    Jake
     
  21. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Can you explain why this is important? E.g. why do you overwrite ServerChangeScene?
     
  22. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    52
    In my case I override ServerChangeScene so that I can provide my own custom scene loading code and display a loading screen with progress bar while it loads. In order to do that I need to have access to s_LoadingSceneAsync which is protected.

    Also just on general principal, if a base class is going to provide an overridable function (ServerChangeScene) then it should also allow the internals of that function to be accessible to anyone overriding the function.
     
  23. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Could you make a pull request on github?
     
  24. Hummy91

    Hummy91

    Joined:
    Jun 7, 2017
    Posts:
    22
    Hey guys, I just picked up Mirror and as I'm a networking novice, I'm pretty much in the dark on setting up a simple system to manage hosts/clients for a lan/lobby GUI system, (I was using Unets default match making system prior).

    Can anyone recommend a lobby asset or something similar from the store that works with Mirror that I could check out as an example to make my own? Or even some documentation/material that'll point me in the right direction of accessing the information needed on the creation of a new host/server and using it to create 'rooms' etc. As I noticed a lot of the functionality provided by Unet by default like NetworkDiscovery has been removed from Mirror, which puts me in the dark on how to go about re-implementing these features. Cheers
     
    Last edited: Dec 10, 2018
  25. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    There is a NetworkLobby asset from Unity on the store, but it has several bugs.
    As far as I know, my uMOBA asset has the only lobby example that works at the moment.

    It's not too difficult to do though, maybe try it yourself. Inherit from NetworkManager and then keep connections in a lobby dictionary or list while they select their team.
     
    Last edited: Dec 10, 2018
  26. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Progress: several smaller fixes and improvements during the last few days. We are also still working on 2018 support, and waiting for the Unity 2018 LTS version.
     
    runningbird likes this.
  27. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Progress: been untangling spawned NetworkIdentities https://github.com/vis2k/Mirror/pull/164/files
    Previously they were stored in NetworkScene. Once for ClientScene and once for NetworkServer (hence twice).

    The change will move all spawned NetworkIdentities to NetworkIdentities.spawned.

    • The name will be more clear, instead of 'NetworkServer.objects'.
    • It will get rid of 71 lines of unnecessary code: upload_2018-12-16_20-2-0.png
    • It will get rid of a lot of unnecessary dictionary lookups and assignments. Should improve performance with large amounts of entities.
    • It will get rid of a lot of complexity, like this SetLocalObject magic:
    • ApplySpawnPayload also required a strange extra GameObject parameter that will be removed after this change.

    In other words: two spawned dicts with a lot of lookups and inserts are now one spawned dict with a lot fewer lookups. These architectural changes are way more important than premature GC optimizations.
     
  28. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    374
    Awesome find! mirror gets better everyday... keep up the great work
     
    vis2k likes this.
  29. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Thanks, we will!
     
    runningbird likes this.
  30. Happycamper8

    Happycamper8

    Joined:
    Aug 31, 2017
    Posts:
    7
    I'm pretty much a one man operation and am an artist. Due to a need I tried to fill, I created a multiplayer training platform using Unity. I demoed to the people in charge and they are excited to push forward. I have been working on other projects and now have deadlines to meet. I now discover the Networking is being deprecated!

    I discovered Mirror and started migrating yesterday. I hope I can get some help and you may have to assume I'm a pretty dumb artist. :)

    The first error I'm getting is the following:
    The best overloaded method match for `Mirror.NetworkServer.AddPlayerForConnection(Mirror.NetworkConnection, UnityEngine.GameObject, System.Guid)' has some invalid arguments.

    From recent comments made here, it seems the playerControllerId has been eliminated? So I took that out of the code and that error seems to have cleared. Is this correct?

    The next error I'm getting is:
    No overload for method `AddPlayer` takes `3` arguments.
    The code is ClientScene.AddPlayer(conn, 0, msg);
    I'm assuming I need to delete the 0, as that seems to clear the error. That 0 is the playerControllerId correct?

    Assuming I'm correct above, that leaves me with 2 errors left I believe.

    Type `Mirror.NetworkConnection` does not contain a definition for `playerControllers` and no extension method `playerControllers` of type `Mirror.NetworkConnection` could be found. Are you missing an assembly reference?

    The code where it's happening is in OnServerDisconnect as follows:
    public override void OnServerDisconnect(NetworkConnection conn)
    {
    var disconnectedClient = conn.playerControllers[0].gameObject;

    Does playerControllers no longer exist? What should I do here? I need to find the gameObject just disconnected.

    Thanks for any help you can offer...
     
  31. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Yes, simply remove playerControllerId parameter.

    Same for AddPlayer, remove the 0.

    NetworkConnection.playerController replaces .playerControllers (just one now).
    So in your case: disconnected = conn.playerController.gameObject;
     
  32. Happycamper8

    Happycamper8

    Joined:
    Aug 31, 2017
    Posts:
    7
    BAM! Thanks so much! That took care of the last 2 errors and allows me to build.

    Now I'm getting an error while running. I start out with a screen where a "player" is chosen. Depending on the player selected in the drop down list, it runs the following in OnClientConnect:
    IntegerMessage msg = new IntegerMessage(chosenCharacter); // This is the index of the character in dropdown
    ClientScene.AddPlayer(conn, msg);

    The error is the following:
    The PlayerPrefab is empty on the NetworkManager. Please setup a PlayerPrefab object.

    I have not set that up in the past since I'm just using the index of the dropdown to correspond to the list of Registered Spawnable Prefabs in the Network Manager.

    To try to solve that, I added one of the characters to the Player Prefab slot under Spawn Info. This caused the error to go away, but only seems to spawn that one character anymore.

    Any ideas?
     
  33. Happycamper8

    Happycamper8

    Joined:
    Aug 31, 2017
    Posts:
    7
    Hmmm, I think what's happening is I had an override for OnServerAddPlayer, which took that msg and spawned the characters.

    I remember now getting this error:
    `NetworkCustom.OnserverAddPlayer(Mirror.NetworkConnection, short, Mirror.NetworkReader)` is marked as an override but no suitable method found to override

    I had removed the override to fix it earlier, but I think this is why my spawning code is not working?
     
  34. Happycamper8

    Happycamper8

    Joined:
    Aug 31, 2017
    Posts:
    7
    Sigh...

    Told ya, I'm a dumb artist. My override still had the playerControllerId parameter...

    Thanks for your help! Seems to be working now!
     
    vis2k likes this.
  35. Happycamper8

    Happycamper8

    Joined:
    Aug 31, 2017
    Posts:
    7
    Man, I wish you had a paypal or something. I don't really know much of anything about patreon. Also, since it's for work, they wouldn't do a monthly payment, but personally I wish I could send some money.

    You have helped me so much. Thanks!
     
  36. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    774
    Soooo, I'm finally looking to make NAT Traversal compatible with Mirror, as several people have requested it now, but I'm hitting a bit of a wall because of this bit:

    NAT Traversal is dependent on running multiple servers, as each punch-through connection can occur on a different port. Also the punch-through connections require a special NetworkConnection class (or at least they did for UNET) to prevent the connection ID's from colliding.

    So, I agree that UNET's approach was cumbersome. Using multiple servers with special NetworkConnections was a crappy hack anyway, but I do need some way to listen on multiple ports on the server. Is there any way to do this in Mirror or could some way be added?
     
  37. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    You can start another telepathy server rather easily and listen on that port.
     
    BiosElement likes this.
  38. RealMTG

    RealMTG

    Joined:
    Jul 27, 2013
    Posts:
    227
    Hi!
    Just wanted to pop by and say that when I tried to join the Discord server, I'm just greeted by an invalid invite screen. "This invite may be expired, or you might not have permission to join." Darn shame as I would like to join it in case I need help! :)
     
  39. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    Here ya go - https://discord.gg/2BvnM4R
     
    RealMTG likes this.
  40. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Which link did you try?
     
  41. RealMTG

    RealMTG

    Joined:
    Jul 27, 2013
    Posts:
    227
    I tried the one on here on the forum post, but I then later realized you need to be logged into Discord in your browser, or else it will just give you an error (that doesn't tell you that you need to be logged in). My bad. :oops:
     
    vis2k likes this.
  42. dimon_babon

    dimon_babon

    Joined:
    Dec 30, 2018
    Posts:
    25
    Hi, if i switch to Mirror, will i still be able to use it in a 'One person is a host and a player simultaneously' way. Or does the server have to be separate from the client in mirror?
     
  43. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Yes, host mode still works!
     
    Last edited: Dec 30, 2018
  44. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    Host mode also works :D
    Of course you still have to deal with NAT traversal, a public NAT facilitator, and a public relay as fallback. This is true regardless of which networking solution you choose.
     
  45. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Or just open the port in your router :)
     
  46. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    Progress: lots of smaller improvements today. Removed a lot of unnecessary code (and possible bugs):
     
    Arganth likes this.
  47. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    39
    Hey I'm upgrading mirror from a september build and just checking something. So that last package I downloaded months back had a telepathy.dll file and the new zip package does not. Do I just delete everything in the mirror folder and replace with the new zip contents?
     
  48. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    175
    @vis2k Final question before using: Is there any option to host "worldwide"/matchmaking servers like in the old UNet?
     
  49. jacknero

    jacknero

    Joined:
    Aug 15, 2018
    Posts:
    14
    HI folks!First of all,very grateful for your going on efforts on the fix and evolution for UNET.

    Since I transport my project to Mirror,the UNET-steam (https://github.com/rempelj/unet-steamworks) plugin I’m using needs a total conversion.For now ,I think I should do it all myself. Due to my poor socket knowledge and experience(as well as English) ,it’ has been really a defy for me.Now I would like to share the work I’ve done and consult you for more help,because actually I got stuck and in fact I’m not sure any of my modification could work indeed.


    1. Mirror has removed everything about channel,so I just delete codes on them,for example ignore the parameters.





    2. I found some fields and attributes just disappear ,so I either replace them with the newer ones (I guess),or just delete them.





    I really don’t if they’re right.



    Since there’s no channel so I guess QoS doesn’t really matter.



    For this one I just can’t find anything looks like, so I delete it.

    3.First I use the release version Mirror and it just can’t connect(localhost,0).

    Since I use 2018.2.18 I change it to the 2018 branch and this socket error is elimated.

    4.Then I found the original unet-steam uses UDP ,afraid of extra compatibility problems caused by TCP.(and there really are,using telepathy will let the initialization stuck at ) So I found Ignorance and put it in.Despite of some little troubles, it helps me push the transport a lot.


    Now the main obstacle in front is this.

    EndOfStreamException: Unable to read beyond the end of the stream.
    System.IO.__Error.EndOfFile () (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    System.IO.BinaryReader.ReadByte () (at <f2e6809acb14476a81f399aeb800f8f2>:0)
    Mirror.NetworkReader.ReadByte () (at Assets/Mirror/Runtime/NetworkReader.cs:21)
    Mirror.NetworkReader.ReadPackedUInt64 () (at Assets/Mirror/Runtime/NetworkReader.cs:71)
    Mirror.NetworkReader.ReadPackedUInt32 () (at Assets/Mirror/Runtime/NetworkReader.cs:61)
    Mirror.Protocol.UnpackMessage (System.Byte[] message, System.UInt16& msgType, System.Byte[]& content) (at Assets/Mirror/Runtime/UNetwork.cs:123)
    Mirror.NetworkConnection.HandleBytes (System.Byte[] buffer) (at Assets/Mirror/Runtime/NetworkConnection.cs:198)
    Mirror.NetworkConnection.TransportReceive (System.Byte[] bytes) (at Assets/Mirror/Runtime/NetworkConnection.cs:273)
    SteamNetworkManager.Update () (at Assets/_Scripts/UNETSteamworks/Network/SteamNetworkManager.cs:206)

    This happens after everything almost done such as the client”connected to UNET server”,”P2P connection established”.Although I can’t tell why this happens and how to solve it ,But I’m sure it’s a lethal error and relates to many very basic things.

    Besides I noticed the connection could disconnected automatically few minutes right after connection established.

    I’m very moved by the passion of discussion and atmosphere here.I hope to get your help and share this good stuff with you sincerely.
     
  50. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,217
    New one has telepathy too. Get Mirror from the asset store maybe, that's the easiest way.

    Mirror is for the game server/client, not for matchmaking servers. Would have to add that yourself or use Unity's upcoming matchmaking service.