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

Unet to com.unity.transport tutorial

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

  1. SpaceMomo

    SpaceMomo

    Joined:
    Sep 30, 2015
    Posts:
    42
    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:
    3,812
    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. SpaceMomo

    SpaceMomo

    Joined:
    Sep 30, 2015
    Posts:
    42
    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.