Search Unity

Unity Multiplayer UNet Sample Projects

Discussion in 'Connected Games' started by seanr, Jun 9, 2015.

  1. TheArchitect485

    TheArchitect485

    Joined:
    Aug 5, 2015
    Posts:
    7
    Having read all these issues, and me having tons of issues with UNet, I hereby decree that UNet is a steaming piece of S***, and we should all find a good alternative. If anyone finds one, plz let me know.
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,607
    Lol I wouldn't go so far as to declare it a waste, it seems to be rough around the edges still, but you gotta remember this just came to be recently, and needs time to incubate like the other networking solutions have had. I assure you photon was not perfect out of the box :)

    That being said, I wish they'd kick it in high gear and have the api flawless asap haha
     
    Munchy2007 likes this.
  3. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    I recently ran a java server and with a c# client I made with sockets. Ran without unity, ran the same inside unity. Vanilla c# ftw! Seems like in the mean time anyone interested in multiplayer stuff might as well go learn networking with sockets and c#.
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    970
    What good C# sockets resources do you recommend? Even though I can search for them, it would be nice to know of your opinions firsthand before delving through the rabbit hole.
     
  5. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    926
    uNet is not as bad as you guys make it sound. There are issues but it's usable and if we report bugs , they reproduce and fix fast. Most of what I've reported are fixed now. Another issue is sometimes you need to play with ConnectionConfig parameters which is not the case with most other solutions but it means you can control networking library and it's needed for many kinds of projects. In terms of alternatives, they exist, there are many alternatives but there are things which uNet can only do. Support of WebGL and a crossplatform game between WebGL and other platforms.
    Congestion control in a proper manner, Some others like photon have some kind of congestion control, @tobbias can describe more , I am not fully aware of the details.

    And uNet has channel kinds which other libraries don't have, like the state sync which tries to update a value as long as it's not hcanged but if changed will try to send the new value instead. Also the fragmented channels and the ability to have/not have sequence, these affect both networking perf and the amount of data sent/received and should not be taken lightly.
     
    Munchy2007 likes this.
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    I don't have any by name. I search for a c# client script, a java server script, made the ports match... and they communicated! I sent a message to the server, the server echoed the message, and then both shutdown.

    In the future I'll try having 2 clients in the server and have their positioned synced and have something basic they can can both interact with and make changes to. Might write the server in GO instead, I've heard that's a lot of fun. Pick your favorite language. If you just want something easy to try out and fast to get a working example, try python.

    The only hard part, if you haven't done something like this before, is setting up the c# client. But you could probably get away with copying a huge chunk of code and just matching the port and ip numbers.

    Eh, what the hey. Here's the important part of the experiment I ran.

    C# client:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Net.Sockets;
    4. using System.Net;
    5. using System.IO;
    6.  
    7. public class NetworkTest : MonoBehaviour {
    8.  
    9.     void Start()
    10.     {
    11.         TcpClient client = new TcpClient("127.0.0.1", 9002);
    12.         try {
    13.             Stream s = client.GetStream();
    14.             StreamReader sr = new StreamReader(s);
    15.             StreamWriter sw = new StreamWriter(s);
    16.             sw.AutoFlush = true;
    17.             while(true) {
    18.                 sw.WriteLine("message from c#");
    19.                 break;
    20.             }
    21.             s.Close();
    22.         } finally {
    23.             client.Close();
    24.         }
    25.     }//start
    26. }//network test
    27.  
    This is java so the syntax highlighting might be off, but here's the server it connected to:

    Code (CSharp):
    1.  
    2. import java.io.BufferedReader;
    3. import java.io.IOException;
    4. import java.io.InputStreamReader;
    5. import java.io.PrintWriter;
    6. import java.net.ServerSocket;
    7. import java.net.Socket;
    8.  
    9. public class Server {
    10.  
    11.     public static void main(String[] args) throws IOException {
    12.        
    13.         ServerSocket serverSocket = null;
    14.  
    15.         try {
    16.              serverSocket = new ServerSocket(9002);
    17.             }
    18.         catch (IOException e)
    19.             {
    20.              System.err.println("Could not listen on port: 9002.");
    21.              System.exit(1);
    22.             }
    23.  
    24.         Socket clientSocket = null;
    25.         System.out.println ("Waiting for connection.....");
    26.  
    27.         try {
    28.              clientSocket = serverSocket.accept();
    29.             }
    30.         catch (IOException e)
    31.             {
    32.              System.err.println("Accept failed.");
    33.              System.exit(1);
    34.             }
    35.  
    36.         System.out.println ("Connection successful");
    37.         System.out.println ("Waiting for input.....");
    38.  
    39.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
    40.                                           true);
    41.         BufferedReader in = new BufferedReader(
    42.                 new InputStreamReader( clientSocket.getInputStream()));
    43.  
    44.         String inputLine;
    45.  
    46.         while ((inputLine = in.readLine()) != null)
    47.             {
    48.              System.out.println ("Server: " + inputLine);
    49.              out.println(inputLine);
    50.  
    51.              if (inputLine.equals("Bye."))
    52.                  break;
    53.             }
    54.  
    55.         out.close();
    56.         in.close();
    57.         clientSocket.close();
    58.         serverSocket.close();
    59.     }
    60. }
    61.  
     
  7. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    926
    Guys writing a game server and client is something much harder if you want to try and make something real. the server should be an async socket server, in java or C# you can try Netty and DotNetty or a wrapper around libuv
    Then you need to have a good and fast serialization protocol , something like thrift or protobuffs and ... and doing it right is not an easy task at all. Otherwise no one would by something like any networking solution. If you are doing it to learn or as a hobby, then go do it.

    Btw if you want to try something cool serverside, try http://github.com/dotnet/orleans. :) or orbit in Java if you are a JMan.
     
  8. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    The idea is that doing it from scratch without having ever done it before could produce better results than what some have experienced with the unet samples :p
     
    xVergilx and tng2903 like this.
  9. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    970
    As for Java, I would simplify the entire thing by using new Thread(Interface.Run()).Start(), where the Interface.Run() refers to an anonymous inner function where you do stuffs asynchronously. Better to start playing around with multithreading capabilities in Java than anything else.

    Also, did I ever mentioned multithreading in Java is more straightforward than other packages? I had more fun with playing with Java threads than in any other languages. Not sure with C# though, but if it's almost straightforward like Java, I could really benefit from this.
     
  10. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    926
    You can pass lambda expressions to Thread.Start in C# but C# doesn't have anonymous classes/interface implementations. The C# version is more clear actually and more concise. The problem is that if you use threads you can produce deadlocks and lots of multithreading bug, on the server using something like above mentioned Orleans would solve this issue with a really nice programming model based on actor frameworks.
     
  11. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    That's what I was going to do. I'm more of a LAN kind of guy and usually play games with 1 other friend on the couch. So I only need 2 threads, one for me and one for unnamed friend. So uhh... if sockets are running on a thread, is there any benefit to using an async socket?

    Java is fun. It's my first language. It's amusing that one of the most verbose languages has one of the easiest implementation for threads.

    Starting with java 8, you can do the same! :D
     
  12. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    926
    I'm not trying to do language comparisons here and you don't need async code if you run it on another thread, it can block.
    I don't like java because of its verbosity as a language but the echosystem around it is better than .NET in terms of available open source libraries in some cases. I strongly hope .NET Core changes that. C# 7 as new features has pattern matching and lots of other great goddies both from functional programming world and not but we can not use them in Unity in any platform other than windows UWP (windows 10 universal platform).

    I really wished it was possible in terms of engineering time and effort to replace IL2CPP with .NET's own coreCLR and AOT compiler but I guess Unity feels the need to control this important part of their technology. If I would like a company to buy Unity for any reason, that would be Microsoft (It's best if nobody buys them but if I have too) and the reason is their new way of looking at things and their .NET core tech.
     
  13. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    That's one of the things I love about it. That might just be because I grew up with it. But I'll probably switch the serve to golang because people are having with with golang as a server technology.
     
  14. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    926
    Yes Go and its goroutines are very interesting for server side programming of micro services. Take a look at Orbit framework or Orleans as well which provide an actor model on Java and .NET worlds for making micro services. Orbit is made by bioware and used for server side of their games and the ideas are all borrowed from Orleans which is created in Microsoft research and is being used for Halo 4, 5 and some other Microsoft and none Microsoft products. Both are open source as well.
     
  15. tng2903

    tng2903

    Joined:
    Apr 8, 2013
    Posts:
    35
    After some struggles, it's really sad to say that you were right. UNET, as it is right now, is not worth the effort. It's been released for a long time, but still the documentation... -.-. UNET has potential, but it's half baked, and the support is very low. May as well written on our own, or bought some asset on the store. Unity as I knew it has been long gone, so sad
     
    Tomnnn likes this.
  16. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    I wish I was wrong. In the time since that post, I've drawn up plans for something interesting. It's going to be a hassle for some people to use I'm sure, but it's...

    Unity => actuate decisions (do physics, get user inputs, etc) and send results to java server
    Java => run local server that makes all of the game decisions (tell clients how to move based on input, give orders to ai, etc)

    Can't wait to see if I end up with a viable strategy :D

    --edit

    Java is just for now, since I have 7 years of experience with it. Might use google GO or something in the future if this proof of concept goes anywhere.
     
  17. AlbertoMastretta

    AlbertoMastretta

    Joined:
    Jan 1, 2015
    Posts:
    63
    Hello everyone.

    I've been using UNET for the project I'm working on, and I did most of the my set up by referencing the TANKS multiplayer lobby example. It all works very well, except for one scenario.

    If I create a lobby, leave, and then create a new lobby, it all seems to be fine until I reach the game. For some reason, the RPC calls that control my game loop don't get called. They work fine whenever I just enter the lobby once, and then go into the gameplay, but if at one point I leave a lobby and enter a new one, they don't work.

    The problem is present in both local hosting and matchmaking.

    I did a test where I removed the [ClientRpc] property from my GameLoop methods, and it works well again, except that it won't be synced up properly on all clients.

    Does anyone have any ideas on how to approach this problem?

    You're help would be greatly appreciated. Thanks.
     
    isidro02139 likes this.
  18. Freakyuno

    Freakyuno

    Joined:
    Jul 22, 2013
    Posts:
    138
    You're not really thinking of it correctly. Your threads are not equal to the amount of players connected, though if you have 2 players connected, you have at least 2 thread for each socket connection, in a real game server you'll have a lot more.

    Think of it this way. Anything your server needs to do...anything, has to be in a thread, because nothing can ever hold up the receipt of the packets on the socket channels.
     
  19. Guhanesh

    Guhanesh

    Joined:
    Aug 3, 2013
    Posts:
    7
    Any new Sample Projects for 5.4 Beta?
    these projects wont work for 5.4
     
  20. neoneper

    neoneper

    Joined:
    Nov 14, 2013
    Posts:
    42
    Hello friends! I'm starting a new project with this new uNET!
    I have some doubts about this concept Local-Client!
    http://docs.unity3d.com/Manual/UNetConcepts.html

    I can not understand how I can avoid cheating on my game because You seeing a customer makes the host job, validating and re-sending information to other customers in the game.

    This logically opens a huge door to defraud the game, using very simple methods, such as Cheat-Engine sample!
    I'm wrong?

    which way to use matchmaking without this vulnerability?
     
  21. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37
    Hello everybody, just made multiplayer project as shown here
    http://docs.unity3d.com/Manual/UNetSetup.html

    then added the project and activated multiplayer as described here
    http://wontonst.blogspot.com/2015/07/guide-to-unity-matchmaking-backend.html

    but players can connect to each other only on local machine, as in the tutorial. Nothing happens when I click LAN Client from two different Android devices. Set host to my public ip.

    How should I config the service so players could connect from different remote devices?
     
    Last edited: Jun 7, 2016
  22. schkorpio

    schkorpio

    Joined:
    Apr 15, 2015
    Posts:
    14
    I would like to know as well. I'm not able to connect between two Windows computers. But works perfectly on the same box.
     
  23. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37
  24. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    347
    A lot of the red errors can be solved using this link:
    https://docs.unity3d.com/Manual/UpgradeGuide54Networking.html
    The API's were changed in 5.4, you just need to add a few extra values to commands ,"",0,""

    Edit: Some such as Invaders project worked without any errors.
    The rest might need a few adjustments to get them updated. :)
    Edit: Spaceshooter needs Other.cs removed and then it works.
     
    Last edited: Feb 24, 2017
  25. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,454
    I'm looking into jumping into a new networking project now. The manual still points to this forum thread for example code, but this thread gives very... erm... mixed reviews of unet. How are people feeling about it since the API changes in 5.4? Is it all working and usable, or would you recommend picking a different solution? (This would be for a turn-based game, so performance isn't as big an issue as ease of setup and maintenance.)
     
  26. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,254
    My recommendation would be GameSparks over UNET currently. I'm basing that on ease of implementation and cost.
     
    GameSparks_Clare and JoeStrout like this.
  27. GameSparks_Clare

    GameSparks_Clare

    Joined:
    Feb 7, 2017
    Posts:
    37
    Thanks for the mention Munchy2007!

    Hi all - if you'd like to give GameSparks a go you can do so for free, we've lots of documents and tutorials to help you get started and if you have any questions or want to discuss your requirements with us before hand, you can get in touch with our Customer Solutions Experts via https://support.gamesparks.net/support/home

    Clare
     
    akademy likes this.
  28. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,454
    Sorry, getting a bit more serious about this now... but GameSparks looks to me like it's not really intended for real-time games (e.g. a multiplayer FPS). Or so it seemed, until I found this.

    Does anybody here have experience using GameSparks for real-time combat?
     
  29. GameSparks_Clare

    GameSparks_Clare

    Joined:
    Feb 7, 2017
    Posts:
    37
  30. div123

    div123

    Joined:
    Jul 18, 2013
    Posts:
    2
    from android to stand alone build data is not getting transferred even if i am connected with same wifi network
     
  31. Koadrack

    Koadrack

    Joined:
    Nov 1, 2017
    Posts:
    1
    Do I need Unity to play these?
     
  32. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    45
    NetworkStarter still does not compile. I get these errors:
    Assets/Lobby/scripts/GuiLobbyControllers.cs(250,22): error CS0122: `UnityEngine.Networking.Match.ListMatchResponse' is inaccessible due to its protection level
    Assets/Lobby/scripts/GuiLobbyControllers.cs(281,19): error CS0122: `UnityEngine.Networking.Match.ListMatchResponse' is inaccessible due to its protection level

    In Unity 5.6.1f1
     
    dmarfurt likes this.
  33. Pawl

    Pawl

    Joined:
    Jun 23, 2013
    Posts:
    110
    Does anyone plan on updating these sample projects to fix the compiler errors? These are still linked from the main Unity Networking documentation: https://docs.unity3d.com/Manual/UNetOverview.html

    I'm seeing the same errors as SavedByZero on Unity 2017.1.1f1

    Sadness =(
     
    dmarfurt, Deleted User and PGI2017 like this.
  34. Jason-H

    Jason-H

    Joined:
    Nov 5, 2010
    Posts:
    87
    @Pawl The networking demo for Tanks!!! is available on the Asset Store under the Tutorial Projects section.

    There is also the Network lobby demo for Meteoroid and probably some other helpful ones if you have a little rummage.
     
    hyaqub_bmt likes this.
  35. sk33lz

    sk33lz

    Joined:
    Jan 24, 2015
    Posts:
    2
    Unfortunately it has compile errors in Unity 2018.3 and all the "new" networking code for Unity 5 is marked as obsolete. It's as if Unity Networking is dead in the water right now, because their own documentation points to using deprecated classes, but isn't even marked deprecated in the 2018.3 documentation.
     
  36. MW_Industries

    MW_Industries

    Joined:
    Jan 20, 2018
    Posts:
    47
    Is Unity Networking still a thing? Or has it just died overtime?
     
    Leoo likes this.
  37. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    970
    The latter.
     
    NeatWolf and MW_Industries like this.
  38. qoly

    qoly

    Joined:
    Jun 18, 2019
    Posts:
    11
    How to create pvp-game now?
     
  39. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    926
    uNet is kinda the answer at the moment on dedicated servers if you are releasing in the current year. When the new netcode becomes stable then that would be the one and I guess its transport later is sorta stable now if you just need that but the situation overall is a bit tricky because uNet is obsolete and will be deprecated when the new tech becomes ready. This said since it is open source, and its LLAPI can be changed to the new one probably easily then if it answers your needs, you don't have to worry much. I released multiple games on it and it works fine for many cases. However PvP is a pretty lose definition so describe more on the number of players and messages per second, gme genre, determinism requirements if any, your simulation model ...
     
  40. qoly

    qoly

    Joined:
    Jun 18, 2019
    Posts:
    11
    I want create simple game, like a chess or something. Two players makes a moves other by other.
     
  41. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    926
    In this case depending on your preferred server language, but a turn based game like chess can be doen in any server language even those made for making websites and REST APIs
     
unityunity