Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Unet to com.unity.transport tutorial

Discussion in 'Connected Games' started by TheLastVertex, Nov 8, 2018.

  1. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    62
    I've been looking over the new networking stuff for com.unity.transport trying to get an understanding of it and think I'm lacking some basic understanding on how to architect classes to work with the new networking API. For reference: https://github.com/Unity-Technologies/multiplayer

    Looking at the Client-Server sample seems pretty straight forward for sending and receiving data, but it leaves a lot of un-answered questions. How do you handle multiple types within a class (It appears to always anticipate sending integers). How do you handle value changes, and know when they occur? What's best practice for triggering events or methods when new data is received? How does a Cmd or RPC get triggered?

    I attempted to dig into the FPS sample to hopefully get an answer to some of these questions but didn't really know where to look to find anything basic enough to start digging into. If you know of any classes I should have a look at please let me know. For reference: https://github.com/Unity-Technologies/FPSSample

    I thought the best way to get an idea of how to handle the new networking would be to try and convert a simple class that uses Unet . Lets use the Unet tutorial for a health class located here: https://unity3d.com/learn/tutorials/topics/multiplayer-networking/networking-player-health

    How would go about translating this using the new networking layer? Code snippet below
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class Health : NetworkBehaviour
    7. {
    8.  
    9.     public const int maxHealth = 100;
    10.  
    11.     [SyncVar(hook = "OnChangeHealth")]
    12.     public int currentHealth = maxHealth;
    13.  
    14.     public RectTransform healthBar;
    15.  
    16.     public void TakeDamage(int amount)
    17.     {
    18.         if (!isServer)
    19.             return;
    20.  
    21.         currentHealth -= amount;
    22.         if (currentHealth <= 0)
    23.         {
    24.             currentHealth = 0;
    25.             Debug.Log("Dead!");
    26.         }
    27.     }
    28.  
    29.     void OnChangeHealth(int health)
    30.     {
    31.         healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
    32.     }
    33. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    4,961
    The new networking API doesn't have any high level API implementation yet, so if you want that you'll have to create it all yourself for now, or wait for a high level implementation from Unity. It doesn't even have reliable messages yet, which you'll probably want for things like SyncVars unless you're changing the value very frequently.
     
  3. TheLastVertex

    TheLastVertex

    Joined:
    Sep 30, 2015
    Posts:
    62
    I understand there isn't a 1-1 translation or even a 10-1. But in the instance of the FPS sample, it has some equivalent of the above code. By equivalent I mean, it has some way to track health, take damage, sync health to clients, and update the UI. I'm looking for a very bare bones start from scratch, what do I need to do to take the old API logic to the new.

    In the FPS sample there is a "HealthState" class, it has a serializer, deserializer, apply damage, and set max health methods. Is this all that is needed? I'm assuming not since there is no reading or writing in that class, but I don't honestly know.

    My understanding from the other deprecation threads and fps sample discussions is, it doesn't seem like there will ever be a HLAPI equivalent. We are going to have to do this all manually at some point or use some sort of "fps template" they've already written for us. If that is the case I'd like to try and learn this as soon as possible, as Unet has shielded me from some basic understanding of what is happening under the hood. I understand this is going to be complicated but I'm assuming (maybe incorrectly) this would be a good jumping off point.
     
  4. ishwara_bhat

    ishwara_bhat

    Joined:
    Oct 18, 2018
    Posts:
    5
    Thanks for this thread. What is frustrating is they have 'flagged old networking as deprecated' in compiler warnings. They should have done it only after there is full example and full tutorial published.. Too much of agile these days.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    4,961
    They did that as a warning that Unet will be removed in the near future. The latest estimate is 2019.2 or 2019.3 as mentioned in another thread.