Search Unity

Unity Multiplayer Mirror - Networking for Unity (UNET Replacement)

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

  1. GuiTeK

    GuiTeK

    Joined:
    Apr 28, 2017
    Posts:
    9
    Hi guys,

    I continue to develop my game with Mirror, and I have a question about the usage of networked game objects, NetworkIdentity and authority.

    I'm trying to use only RPCs (and no network messages) as it seems it is the recommended way.

    What I want to achieve: call an RPC from the client to the server in a non-player GameObject.

    Here is what my game looks like:
    • LoginScene [Scene]
    • CharacterCreationScene [Scene]
      • CharacterCreationManager [Empty GameObject]
        • NetworkIdentity [Script Component] -- "Local Player Authority" is checked
        • CharacterCreationManager [Script Component]

    What works:
    1. CLIENT: LoginScene shows up, the user types their username/password and click "Login" button
    2. SERVER: checks the username/password in OnServerAddPlayer() and if they are correct, sends a SceneMessage("CharacterCreationScene") to the client
    3. CLIENT: CharacterCreationScene shows up, the user types their nickname and click "Create" button, C.F. below
    Code (CSharp):
    1. using Mirror;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. namespace Scripts.Scenes.CharacterCreationScene
    6. {
    7.     public class CharacterCreationManager : NetworkBehaviour
    8.     {
    9.         public InputField NicknameInputField;
    10.  
    11.  
    12.         public void OnCreateButtonClick()
    13.         {
    14.             NetworkIdentity networkIdentity = GetComponent<NetworkIdentity>();
    15.  
    16.             CmdCreateCharacter(networkIdentity, NicknameInputField.text);
    17.         }
    18.  
    19.         [Command]
    20.         private void CmdCreateCharacter(NetworkIdentity networkIdentity, string nickname)
    21.         {
    22.             Debug.Log($"Creating {nickname}...");
    23.         }
    24.     }
    25. }
    26.  
    However, when I click on the button, I get the following warning and the function isn't called on the server:
    Trying to send command for object without authority.

    So I have two questions:
    1. Why can't I call a RPC even if the "Local Player Authority" is checked on the NetworkIdentity component of the GameObject? How can I fix this problem?
    2. What would be the recommended way/pattern to do what I'm trying to do (if I'm not doing it the right way)?

    Thank you so much guys!
     
  2. krkim_unity

    krkim_unity

    Joined:
    Mar 30, 2018
    Posts:
    5
    Hi
    Mirrors are a great asset.
    I was really impressed with this asset. best!!
    I am trying to put a multi into a sandbox game using a mirror.
    However, I faced great difficulty.
    I was not good at synchronizing the world.
    The world is stored as a class type and is declared as a 2D array because it is a 2D world.
    However, when I read the documentation, I could not find a way to synchronize the class types.
    When you play a multiplayer game, you need advice on how to make the terrain the same.
    We need advice on how to efficiently synchronize class information.
    I am not a person living in an English-speaking country, so the question may be strange.
    Sorry, please understand.
    I will wait for your answer.
    thank!
     
  3. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    Short answer is: you don't. The correct pattern is to have all the code that communicates player actions / inputs on your player prefab. That means when you open a UI from your player you need to subscribe to the UI events in your player code.

    Stop by our Discord to get faster help.
     
  4. ScottPeal

    ScottPeal

    Joined:
    Jan 14, 2013
    Posts:
    25
    Has anyone gotten Mirror to run on 2019.2 alpha in a hybrid ECS mode?
     
  5. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Internal improvement: previously, UNET would send a spawn message for each entity, then afterwards one additional OwnerMessage to let the client know which of the entities is the local player. This was a huge bandwidth overhead (especially if it takes one extra TCP packet) and caused race conditions where isLocalPlayer may or may not be set in Start() already.

    We fixed it by storing the owner information in the spawn message directly. No more extra packet, no more extra code path, no more race condition.
    Nice find @goldbug

    We highly recommend using Unity 2018.4 LTS when it's released. Even 2018 isn't stable yet, I wouldn't bother with 2019 for a game server. It needs to be stable.
     
    SwiftIllusion likes this.
  6. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Cecil Update: the Weaver originally used Unity's older custom Unity.Cecil version to replace IL code for Rpcs/Cmds/Serialization.

    Today we upgraded it to the latest official Cecil 0.10.3 ( https://github.com/jbevain/cecil/ ) thanks to c6burns .

    This should give us several important bug fixes for the weaver, as well as peace of mind because there is one less magical part in the chain.

    We also fixed one recursion stack overflow bug in Cecil today, which allows us to further dig into yet another Weaver bug.
     
  7. YusukeTakehara

    YusukeTakehara

    Joined:
    Mar 13, 2018
    Posts:
    2
    Hi!
    I started using this as an alternative to UNET

    I want to use multiple clients for different purposes.
    Can not use multiple clients connected to different servers?
     
  8. YuriPetskus

    YuriPetskus

    Joined:
    Jun 22, 2018
    Posts:
    16
    After the client disconnects from the server and reconnects, the client receives exceptions such as:
     
  9. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    What do you mean exactly?

    Please open an issue on our github and let us know exactly how to reproduce it :)
     
  10. Vancete

    Vancete

    Joined:
    May 3, 2010
    Posts:
    180
    What's the flow to send login credentials from client to server, then check it on the server, then send OK to login to the client? All before spawning all the network objects.

    Explaining that on the docs would be interesting I think.

    Thanks
     
  11. YusukeTakehara

    YusukeTakehara

    Joined:
    Mar 13, 2018
    Posts:
    2

    Thank you for your reply!
    I want to use multiple clients for different purposes at the same time.
    So,It is not good to reconnect after disconnecting one of the connections.

    My implementation is as follows by UNET.

    Code (CSharp):
    1. public NetworkClient myClient1 = new NetworkClient();
    2. public NetworkClient myClient2 = new NetworkClient();
    3. myClient1.Connect(connectServerIP1, connectServerport1);
    4. myClient2.Connect(connectServerIP2, connectServerport2);
     
  12. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    We did!
     
    Vancete likes this.
  13. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    NetworkClient is a singleton in Mirror. There can be only one.

    That said, perhaps there's a better way to accomplish what you need.
     
  14. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,137
    No, that will just delay the player object being spawned until authenticated. He wants to do authentication before any objects, state and other information is distributed. Essentially isolated.
     
  15. Vancete

    Vancete

    Joined:
    May 3, 2010
    Posts:
    180
    That's coool!
     
  16. yottabyte2k4

    yottabyte2k4

    Joined:
    Mar 3, 2018
    Posts:
    5
    Just wanted to say THANK YOU for all the development effort behind Mirror! I'm just getting up to speed with game development and made a multiplayer local co-op prototype that I wanted to transition to drop-in online/local hybrid. I have to say I felt like I was in a tough spot between the upcoming (and largely undocumented) new networking API and UNET being deprecated.

    I liked what I saw of UNET but didn't want to learn an abandoned platform... so decided to get up to speed on Mirror and I'm really liking it so far! I was a bit saddened (initially) to see the drop of playerControllerID for having multiple controllers per client as I'm intending on drop-in local + online coop. I might suggest you remove the reference to that in the docs here since that tripped me up for a while: https://vis2k.github.io/Mirror/Concepts/GameObjects/SpawnPlayerCustom until I found your posts mentioning removing it.

    The solution I've developed so far is to use spawn an empty as the playerPrefab (representing the client) that has a script on it that can spawn however many local player models as game objects with client authority. It seems to be working great so far. I may put together a tutorial of the process once I get the kinks ironed out if anyone else ends up in the same situation.

    I'm also very interested in your List Server product once I get this project farther along and will likely give it a try. If you ever plan on adding punchthrough (or even some general guidelines on how to achieve that) that would make it even more valuable.

    Anyway thanks again!
     
    vis2k likes this.
  17. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Great to hear, thank you!
    We really love the UNET architecture, and we try very hard to get it perfect.

    You can try the list server yourself, Mirror comes with an Examples/List Server scene that connects to it. The server hasn't crashed since day one, and you can even host it for free on a google cloud f1-micro instance.
     
    Last edited: Apr 16, 2019
  18. Vares

    Vares

    Joined:
    Aug 20, 2017
    Posts:
    2
    Hey, thanks for your work. Is there a way to temporarily disable weaver while i am working in the editor/debugging. Weaver makes my visual studio project undebuggable as the breakpoint locations cant be found (I assume because breakpoint locations dont match the Assembly after weaving). I don't understand exactly where it is invoked from and the only solution I have found is to delete the weaver folder entirely...
     
  19. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Try the latest mirror version from github. We fixed that breakpoint issue a few days ago.
     
  20. timsp

    timsp

    Joined:
    Jul 6, 2018
    Posts:
    1
    Hello, I'm converting a working UNET project to Mirror and am having trouble loading scenes when the built exe is the client
    I can load scenes between two editor sessions and can load a scene when the built exe is the host and the editor is the client. However, The load fails on the client when the editor is host and the exe is client.
    I have the scene-to-be-loaded in the build list. Is there some other step I have to take to register a scene as loadable?
    Unity 2018.3.12f1, Mirror master (b4c9c6f). Also tried with asset store version.

    /edit - exe host and exe client works, so this is a minor issue that only affects the case where exe is client and editor is host.

    /edit again - we are having a similar issue when running two editors on two machines, which is needed for our applications. We were able to do this with UNET. I'm guessing the hash/id for the scenes is different, though the data is identical. Is this a known issue? Any pointers to look in the code to see how/where the id is generated? thanks

    /possible resolution - We had not been using Online scene with UNET, and had nearly all our gameobjects in our 'main' scene. I realized that existing NetworkIdentity components in this Main scene were not getting assigned networkids on the clients. UNET used to correctly give existing these components ids, Mirror does not appear to.
    Workaround is a 'main' scene with only the NetworkManager and make our old main scene the online scene (with the NetworkManager removed) With this change, networkIdentities in the online scene are getting netids.

    Partial log in spoiler:
    Spawn scene object not found for D859ACD4 SpawnableObjects.Count=0
    UnityEngine.Debug:LogError(Object)
    Mirror.ClientScene:OnSpawnSceneObject(NetworkConnection, SpawnSceneObjectMessage) (at Assets/Mirror/Runtime/ClientScene.cs:405)
    Mirror.<>c__DisplayClass6_0`1:<MessageHandler>b__0(NetworkMessage) (at Assets/Mirror/Runtime/MessagePacker.cs:128)
    Mirror.NetworkConnection:InvokeHandler(Int32, NetworkReader) (at Assets/Mirror/Runtime/NetworkConnection.cs:205)
    Mirror.NetworkConnection:TransportReceive(Byte[]) (at Assets/Mirror/Runtime/NetworkConnection.cs:236)
    Mirror.NetworkClient:OnDataReceived(Byte[]) (at Assets/Mirror/Runtime/NetworkClient.cs:126)
    UnityEngine.Events.UnityEvent`1:Invoke(Byte[])
    Mirror.TelepathyTransport:processClientMessage() (at Assets/Mirror/Runtime/Transport/TelepathyTransport.cs:69)
    Mirror.TelepathyTransport:LateUpdate() (at Assets/Mirror/Runtime/Transport/TelepathyTransport.cs:96)
     
    Last edited: Apr 18, 2019
  21. Alexees

    Alexees

    Joined:
    Nov 8, 2017
    Posts:
    187
    @vis2k you mentioned this some time ago. How would you go about it? copy paste the component from the Unity Bitbucket and convert it?
     
  22. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Check out the UDP transports that work with Mirror. LiteNetLib offers it, ENET maybe too
     
  23. honzapat

    honzapat

    Joined:
    Oct 3, 2015
    Posts:
    18
    Scene loading does not work in the newest version
    Editor:2018.3.11f1
    Mirror:Newest to this date on assetstore
    NetworkManager wont switch to online scene from offline scene and vica versa and completely scene managin seems F***edup.
     
  24. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    News: POPULATION: ONE by BigBoxVR added to our Showcase!


    Thanks to the BigBoxVR guys for trusting us with their networking.
    Makes us insanely happy to see Mirror being used in action!
     
    Hypertectonic, HeadClot88 and hopeful like this.
  25. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    Cannot reproduce...here's what I tried:

    Starting with the Additive Scenes example, I created an offline scene, moved the Additive Network Manager to that scene, and moved the subscene loading code from OnStartServer to OnServerSceneChanged so it would only run in the MainScene, added the offline scene as first in the build settings, and assigned the offline and main scenes to the Additive Network Manager, built and run. Start Host in editor, built app as client. Both switch to the game scene and offline scene correctly as expected when clicking the appropriate start and stop buttons in the HUD.

    Drop by our Discord and we'll try to help figure out where you went wrong.
     
  26. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    This was resolved in Discord. User had a Network Manager in all scenes instead of just the first (offline) scene.
     
    vis2k likes this.
  27. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,847
    How would I go about implementing network discovery across LAN? Trying to get players to auto find each other + be visible in a lobby list if they are on the same network
     
  28. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Check out our UDP transports, most of them support network discovery!
     
    GameDevCouple_I likes this.
  29. Lork

    Lork

    Joined:
    Jul 12, 2013
    Posts:
    63
    Is there a universal way to tell whether code is running on a client or the server? The isServer and isClient properties of NetworkBehaviour are the only ones I've found, but they require access to a specific instance of a NetworkBehaviour and seem to get set very late, which makes them not very useful for setting things up.
     
  30. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    296
    NetworkServer.active
    NetworkClient.active
     
  31. Lork

    Lork

    Joined:
    Jul 12, 2013
    Posts:
    63
    Thanks.

    Is there a recommended way to determine when all clients are "ready? ie. player objects are registered with the server, all network gameobjects have been spawned and their SyncVars have been initialized.

    Right now I'm having one of my NetworkBehaviours send a command when a certain SyncVar changes from its default value, but I suspect there's a better way to do it.
     
  32. Katori

    Katori

    Joined:
    Nov 30, 2009
    Posts:
    60
    I just released RocketSockets, a WebSockets transport for Mirror that runs totally outside of Unity.

    The key difference in RocketSockets and existing transports is not only that it runs outside of Unity, but it also allows you to run and manage multiple WebSockets servers from a single server binary, eliminating the overhead of running multiple local and remote connections in multiple binary instances. I've also seen reduced CPU and RAM on the Unity side in my tests with my game, since Unity isn't doing the networking work anymore. And of course, it supports unlimited CCU - as many as your hardware can handle, and I'm pretty confident it can handle a lot more with this than with any other WebSockets solution that runs inside Unity.

    In celebration of and exclusively for the Unity forums I have two 10% off coupon codes for Unity forum users. Please reply or PM me if you use them so I can strike them through.
    UNITYFORUM01
    UNITYFORUM02

    Feel free to let me know here or in the Mirror Discord if you have any feedback about RS, I have some great stuff coming for it.
     
  33. yottabyte2k4

    yottabyte2k4

    Joined:
    Mar 3, 2018
    Posts:
    5
    I've been struggling with finding answers to a couple things (maybe more UNET related in general) and was hoping someone might be able to chime in:

    1) How does "Server tick rate" under "Network Manager" script relate to "Network Sync Interval" that is available on individual scripts that have SyncVars ?

    My current assumption is that the sync interval of individual scripts can be slower than but not faster than the server tick rate but I'd like to confirm that

    2) SyncVars only update on change, correct?
    I believe that's what I gathered from this old UNET blog https://blogs.unity3d.com/2014/05/29/unet-syncvar/

    I just want to make sure I'm not using up tons of bandwidth using a whole bunch of SyncVars for what basically amounts to some initialization. For SyncVars that don't really get used again or very sporadically.

    3) For sending (fairly) large amounts of data- Are multiple SyncVars fairly well optimized?
    Suppose I have a handful of various data types like booleans, ints, gameObjects and colors I need to sync up between players. Are separate SyncVars wasteful for that?

    Or would I get any benefit from looking into SyncLists, or Serializing and Deserializing manually?

    4) Are there any examples out there of using other transport layers? (specifically UDP based Ignorance or LiteNetLib4Mirror?)

    Specifically I'm curious about channel configuration (reliable vs unreliable) and management.

    I plan to start digging through the source code so I can answer some of these questions myself but thought I would reach out

    I've already been able to play online with friends so I'm miles ahead of where I would be without Mirror! Although I am consuming more bandwidth than I expected which is why I'm starting to dig deeper into this stuff.
     
    HeadClot88 likes this.
  34. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    684
    The Server tick rate simply sets the frame rate of headless servers. That is all. Ideally we would not mess with that at all, but the default behavior from unity is just asinine and causes headless servers to suck up a lot of CPU doing nothing.

    The Network Sync Interval determines how often syncvars are synchronized. You can change synvars 100 times in a short amount of time, but Mirror will only synchronize the last value when the interval expires.

    The server tick rate determines how often the Update() function is called in the server, we can't synchronize variables any faster than that, so the server tick rate imposes a lower limit for the sync interval.

    Yes. SyncVars only synchronize when they change. If an object does not change, no message is sent.

    Syncvars are highly optimized for this. Suppose you have 30 syncvar variables in a component. Suppose you change 5 of them. When the sync interval is met, Mirror will generate one message with the 5 variables that changed plus a small bit mask.

    Having separate syncvars allows mirror to batch them together and only synchronize the ones that changed.

    https://github.com/SoftwareGuy/Ignorance/tree/master/Assets/Demo

    We could really use some sort of profiling tool. It is hard to determine what consumes bandwidth.
     
  35. merckerrobert

    merckerrobert

    Joined:
    Jun 22, 2017
    Posts:
    4

    Does this allow the use of networkidentity components to be on dontdestroyonload game objects?
     
  36. Alexees

    Alexees

    Joined:
    Nov 8, 2017
    Posts:
    187
    @vis2k How do I get help on "Could not find scene object with sceneid" issues? In Discord Help channel nobody answered my question.
    I do have a little problem with I guess different scene setups on server and client. My server needs to only load 3 scenes while clients additionally load environments. Now I get "Could not find scene object with sceneid" warnings. Do I have to make sure particular scenes are listed in a certain order, a particular scene needs to be set active, or what do I have to check to get this working alright? My setup comes from UNET and it worked there.
     
  37. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    If you think you found a bug then please create a small project where we can reproduce it
     
  38. Alexees

    Alexees

    Joined:
    Nov 8, 2017
    Posts:
    187
    I don't think I did. I guess it's just something with my setup, but I have no idea how to validate it. How do I go from these errors to why are they not found?
    I'm not even sure what gameobjects they are.
     
    Last edited: May 14, 2019
  39. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Can you explain your problem with as much detail as possible? Ideally a video
     
  40. merckerrobert

    merckerrobert

    Joined:
    Jun 22, 2017
    Posts:
    4
    @Alexees are you using networkidentity with don't destroy on load objects, that causes issues with mine.

    @vis2k does the most recent mirror release support networkidentity with dontdestroyonload objects?
     
  41. Alexees

    Alexees

    Joined:
    Nov 8, 2017
    Posts:
    187
    Nevermind for now. I was using the AdditiveNetworkManager without loading additively on clients cause I thought that's what it would automatically do. And now need to delay the ClientScene ready until that is done because of the network calls coming in too early for the scene gameobjects to be available, which caused the problem.
     
  42. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Progress Update: the giant wave of features/modifications has finally slowed down, so expect fewer breaking changes in the next couple of weeks. We are focusing on open issues and minor improvements now!

    Try and let us know :)
    Scene switching is very complex. We got it to work reasonably well now, but there might still be some edge cases left.
     
    HeadClot88 likes this.
  43. Alexees

    Alexees

    Joined:
    Nov 8, 2017
    Posts:
    187
    I would never do that, it sounds like a really bad idea.
     
  44. Andy3D

    Andy3D

    Joined:
    Jan 31, 2015
    Posts:
    19
    Hope this is the right place to ask. Im developing an app for Oculus Go using Unity, and need some way of managing options within the app from an external Android tablet. Basically, to hide/show scene buttons in the lobby, start scenes up, and send a couple of setup variables across to the headset, record some basic session info from the headset)
    Ideally, it could be two versions of the same app - with the admin app being the server, and the headset being the client. Then having the admin be able to show/hide buttons, whilst the headset can just click whats been enabled.
    Anyone have any idea on which of the many networking solutions would be a good fit?
    Thanks a lot
     
  45. merckerrobert

    merckerrobert

    Joined:
    Jun 22, 2017
    Posts:
    4
    @vis2k turns out mirror still doesn't support using DontDestroyOnLoad and NetworkIdentity, but if someone wanted to make mirror support it they would need to make it so the network identity knows its a dontDestroyOnLoad and have the person give it a unique string for hashing. Also the networkscenepostprocess.cs would need to allow for dontDestroyOnLoad objects to be found in resources. However there is still the issue of returning to the original scene with said object.
     
  46. xxUnlinkxx

    xxUnlinkxx

    Joined:
    May 10, 2019
    Posts:
    1
    Can someone help? for some reason when I try to use OnStartClient() it says this in the console:
    Exception in OnStartClient:Object reference not set to an instance of an object at Mirror.Examples.NetworkLobby.NetworkLobbyPlayerExt.OnStartClient () [0x00065]

    NetworkLobbyPlayerExt.cs:27
     
  47. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    Takek a look at NetworkLobbyPlayerExt.cs:27 - there is probably a null value where it shouldn't.
     
  48. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,115
    I'm pretty new to networking and am stumbling across the terror that is the uNet situation. Mirror looks like an awesome fix for a 2yr old project that's heavily invested in MonoBehaviour. The only thing is it's using matchmaking to set up its online games. Will Mirror work with matchmaking?
     
  49. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,237
    We don't use any of the Unity services. I am not sure if they are even supported anymore.
    But you can create your own matchmaking if needed.
     
  50. stanislavdol

    stanislavdol

    Joined:
    Aug 3, 2013
    Posts:
    269
    Hi, first of all, this is an amazing project, finally, a relatively easy way to get an authoritative server with authoritative game logic.

    Unfortunately, I am not very familiar with server hardware requirements.
    In your uSurvival test you used:

    Xeon W-2145 8 Cores x 3.7 GHz
    128 GB DDR3
    240 GB SSD, 6GB/s
    Ubuntu 16.04 LTS

    Considering that it was the worst case scenario, is it safe to assume that it should be sufficient for 300-400 CCU?
    But for a smaller,room based game(matchmaking is not an issue) with let's say under 16 players per room, what setup should I look for? Will vps be sufficient, considering that server runs a unity instance and has to make some physics calculations?
    Is there any practical way to estimate that?
    Also, is there a significant performance advantage of using ubuntu instead of windows?
    Thank you.
     
    vis2k likes this.