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. 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:
    2
    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:
    275
    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,042
    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.
     
  6. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,042
    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:
    12
    After the client disconnects from the server and reconnects, the client receives exceptions such as:
     
  9. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,042
    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:
    173
    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:
    275
    We did!
     
    Vancete likes this.
  13. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    275
    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,065
    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:
    173
    That's coool!
     
  16. yottabyte2k4

    yottabyte2k4

    Joined:
    Mar 3, 2018
    Posts:
    1
    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,042
    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:
    1
    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,042
    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 at 11:24 PM