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

Showcase Mirror - Open Source Networking for Unity

Discussion in 'Multiplayer' started by mischa2k, Aug 11, 2016.

  1. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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.
     
  2. electricfalconllc

    electricfalconllc

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

    mischa2k

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

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    126
    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?
     
    mischa2k likes this.
  5. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    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
    crandellbr and mischa2k like this.
  6. Player7

    Player7

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

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    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.
     
  8. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    31
    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
     
  9. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    @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.
     
  10. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    31
     
  11. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    31
    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
     
  12. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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.
     
  13. mischa2k

    mischa2k

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

    Player7

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

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    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.
     
  16. Player7

    Player7

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

    validname1

    Joined:
    Feb 1, 2018
    Posts:
    26
    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.
     
    mischa2k likes this.
  18. validname1

    validname1

    Joined:
    Feb 1, 2018
    Posts:
    26
    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
  19. bug5532

    bug5532

    Joined:
    Aug 16, 2011
    Posts:
    307
    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.
  20. yusufdemir2012

    yusufdemir2012

    Joined:
    Dec 21, 2013
    Posts:
    50
    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?
     
  21. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Use the Asset Store project
     
  22. yusufdemir2012

    yusufdemir2012

    Joined:
    Dec 21, 2013
    Posts:
    50
    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?
     
  23. 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?
     
  24. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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.
     
  25. 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.
     
  26. mischa2k

    mischa2k

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

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    mischa2k likes this.
  28. bug5532

    bug5532

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

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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
  30. MCoburn

    MCoburn

    Joined:
    Feb 27, 2014
    Posts:
    71
    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.
  31. blacksun666

    blacksun666

    Joined:
    Dec 17, 2015
    Posts:
    214
    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.
     
  32. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533

    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.
     
  33. MCoburn

    MCoburn

    Joined:
    Feb 27, 2014
    Posts:
    71
    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.
     
  34. mischa2k

    mischa2k

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

    Player7

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

    Player7

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

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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..
     
  38. Player7

    Player7

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

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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.
     
  40. FizzCube

    FizzCube

    Joined:
    Feb 8, 2014
    Posts:
    5
    nirvanajie, mischa2k and Player7 like this.
  41. Fiffe

    Fiffe

    Joined:
    Dec 10, 2014
    Posts:
    19
    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?
     
  42. 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.
     
  43. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    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.
  44. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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).
     
  45. 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?
     
  46. jinnindo

    jinnindo

    Joined:
    Dec 8, 2015
    Posts:
    17
    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?
     
  47. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    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.
  48. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    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?
     
  49. antsonthetree

    antsonthetree

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

    mischa2k

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

    Depends on the transport that you use.