Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Networking?

Discussion in 'General Discussion' started by ExDeaDguY, Nov 3, 2018.

  1. ExDeaDguY

    ExDeaDguY

    Joined:
    Aug 25, 2009
    Posts:
    455
    Hey guys, This has most likely been answered already... But I can't find anything that applies at this moment in time and is relevant. So I have started working on a project that I have been taking a break from, and have decided it's time to start working on the networking aspect... Unity's documentation basically says "Do not use UNET as it will be obsolete in upcoming versions", and no replacement is stated... So what should I use for networking?

    I've looked at external tools but I am not sure what is best for my needs. I will need something that most likely peer 2 peer and will host around 8 players per match / game.

    I was thinking Photon Cloud (Realtime) would be a good option because I have experience with it, but I'd rather not use a pay to use service if Unity is coming out with a networking solution sometime soon. Has anyone uncovered more information on this subject?

    Thanks.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,002
    ExDeaDguY likes this.
  3. NSdesignGames

    NSdesignGames

    Joined:
    Dec 29, 2010
    Posts:
    488
    Hey,
    I know you said you don't want to use Photon Cloud because it's paid. However currently they offer a one-time pricing plan. Basically you pay once and have up to 100 Concurrent Users for 60 months(!)
    The price is quite low, you can check it here: https://www.photonengine.com/en/realtime/pricing
    Don't forget that there is always a free plan for 20 Concurrent Users which is good for testing.

    Also while we at it, I wrote a beginners guide for Photon Network, feel free to check it here: https://sharpcoderr.com/blog/photon-network-beginners-guide
    It explains how to create a lobby with a Room creation/selection feature and a simple position sync while in the room.

    (Yeah basically I wrote this to promote my blog post but hopefully you or someone else will find it useful).
     
    ExDeaDguY and Antypodish like this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,814
  5. ExDeaDguY

    ExDeaDguY

    Joined:
    Aug 25, 2009
    Posts:
    455

    I'm following your guide to get started as I haven't used Photon in a couple years... There are a bunch of "Photon" projects in the asset store now. Would I want to grab the one listed as "Classic"? I assume this refers to the old one. I need the realtime one that can support multiple rooms / lobbies.

    Apparently there's PUN 2 now. I'm not sure what that applies to or what is supported and such.
     
  6. ExDeaDguY

    ExDeaDguY

    Joined:
    Aug 25, 2009
    Posts:
    455
    I'm looking into another avenue just in case. I've been working with Photon and I'm not really sure that's what I need. I can write an API and service calls pretty good when it comes to web development, such as... HTTPRequests and responses and whatnot. If I was to set up a custom networked API and have my own server handling users- How would I manage packets being sent back and forth? How do you specifically send a packet and respond to it? Or would someone use an http protocol for this and call the HTTP request from unity and return it from WEBAPI?
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    12,436
    You can send data back and forth using sockets. They've been part of the .NET and Mono frameworks since the very beginning and under the hood they're the way every one of these networking APIs functions.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket?view=netframework-3.5
    https://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using
     
    ExDeaDguY and vakabaka like this.
  8. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    2,091
    To elaborate more on the idea of creating a custom networking solution, use UDP sockets in C#. With UDP sockets, both ends are a host with an endpoint of IP and port. It is not like TCP where one end is a server and the other is a client. Also, with a UDP socket, you send individual packets instead of streaming the data. You can figure out exactly what you want in each packet, byte by byte, and create your own data serializer/deserializer code. It can be a perfect fit for your game when you build it all yourself instead of trying to use a generic networking solution.

    If the game is going to be peer to peer, read about STUN. That is a fairly standard method for doing peer to peer gaming networking through client side NATs.

    https://en.wikipedia.org/wiki/STUN
     
    vakabaka and Joe-Censored like this.
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,832
    On the UDP sockets topic, it isn't all that difficult to take the connectionless host nature of UDP sockets and build your own connection oriented client/server functionality on top of it, nor is it really that difficult to want to send arbitrary amounts of data and chop it up on one end then recombine on the other in a reliable manner even with UDP's size limits and lack of built in reliability. If your game needs those kinds of things, they can be implemented. Though if that is most of what you need, then TCP might be a better choice since that comes built in by default.
     
  10. NSdesignGames

    NSdesignGames

    Joined:
    Dec 29, 2010
    Posts:
    488
    Thanks for pointing out. The guide indeed applies to the older version of Photon, I have updated the title.

    Basically 'Photon Unity Networking Free' is now 'Photon Unity Networking Classic - FREE' and 'PUN 2 - FREE' is a newer version, which according to devs, was released as a separated version because it undergone an internal restructuring and the changes were far too great.
    Their aim with PUN 2 is to improve performance and reduce memory consumption (Garbage Collection).

    I'll be writing a guide for PUN 2 soon. Should be ready in couple days (I'll post the link here once it's done).
     
    ExDeaDguY likes this.
  11. ExDeaDguY

    ExDeaDguY

    Joined:
    Aug 25, 2009
    Posts:
    455
    I just wanted to give an update on this thread and say Thank You to everyone for the suggestions.
    I ended up writing all of the code from scratch, Created a WebAPI project which acts as the master server. (Access to sql server for saving users, and registering list of instance servers) and I created a second project (Console App) that uses system.network.sockets (TCP/UDP). The TCP is working correctly and data can now be sent back and forth (And access the webapi to retrieve user information), the next step is setting up UDP calls for things such as movement.

    At this point, I will only need a dedicated server in which IIS will host the WebAPI, a few instances of the headless server. (Which may get integrated into unity if I decide to let this be hosted by players).

    Cheers!
     
  12. tiggus

    tiggus

    Joined:
    Sep 2, 2010
    Posts:
    1,170
    That's pretty much how I did it as well, a basic rest api for authentication which generates a session token, and then the game server is udp and validates the session token when client joins. Basic but it works.
     
    ExDeaDguY likes this.