Feiner, COMS W4172, Spring 2018
Introduction to Networking in Unity
Carmine ElvezioProf. Steve Feiner
COMS 4172March 22, 2018
Basic Networking Concepts
Server/Client communication handled by Unity
Clients connect to listening server
Single server—multiple clients
Unity applications can be configured as server, client, or host
Hosts in UNET are servers with a local client instance
Clients control local “players”
Players can be configured to have local authority
2
Feiner, COMS W4172, Spring 2018
Networking in Unity
New networking system introduced in 2015: UNET
Older, deprecated net framework still present—do not use!
Deprecated components are labeled
UNET
High-level scripting API (HLAPI)
Lower-level transport API
3
UNET
http://docs.unity3d.com/Manual/UNetUsingHLAPI.html4
Feiner, COMS W4172, Spring 2018
NetworkManager
The starting point of a basic networked application—use it!
This can be used to easily configure both server and client through a single component
Exposes settings for player, other prefab objects, and some network transmission settings
http://docs.unity3d.com/Manual/UNetManager.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkManager.html 5
NetworkManager
Network Address
Client will try to connect to this IP over specified port
Server Bind To IP
Server can listen to specific IP address
Can configure matchmaking settings from this component as well
Derive from NetworkManager for more advanced control over settings & callbacks
http://docs.unity3d.com/Manual/UNetManager.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkManager.html 6
Feiner, COMS W4172, Spring 2018
NetworkManager
Enable Advanced Configuration to tune performance for your application
Add/Remove/Edit QoS Channels to specify behavior per channel
Unreliable—send message with no guarantees of receipt
Reliable—All messages will be sent, may slow updates
AllCostDelivery,ReliableFragmented, etc.
http://docs.unity3d.com/Manual/UNetManager.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkManager.htmlhttp://docs.unity3d.com/ScriptReference/Networking.QosType.htmlhttp://blogs.unity3d.com/2014/06/11/all-about-the-unity-networking-transport-layer/ 7
NetworkHUD
Very convenient GUI that can be used in all deployments to easily control network settings at runtime
Also has editor controls
Click LAN Host to create Server/Client application
Click LAN Server Only to create server
Click LAN client to create client
Need to specify target IP
http://docs.unity3d.com/Manual/class-NetworkManagerHUD.html8
Feiner, COMS W4172, Spring 2018
NetworkServer
Get Handle to a NetworkManager in the scene
GameObject.GetComponent<NetworkManager>()
Start Server with NetworkManagerName.StartHost() or NetworkManagerName.StartServer()
Get access to server through NetworkServer
NetworkManager provides handles to some NetworkServer features
Alternatively, create NetworkServer object and start with NetworkServer.Listen(PortNumber)
Register Handlers with NetworkServer.RegisterHandler(MsgType.Type, FuncName)
http://docs.unity3d.com/Manual/class-NetworkServer.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkServer.html 9
NetworkClient
Get Handle to a NetworkManager in the scene
GameObject.GetComponent<NetworkManager>()
Start Client with NetworkManagerName.StartHost() or NetworkManagerName.StartClient()
Get access to client through NetworkManager.Client, which returns client object, or NetworkClient.Instance
You can also configure an external client (useful for assigning specific instantiation behavior)
Alternatively, start system using NetworkClient.Connect(IPAddress, Port)
Register Handlers with ClientObj.RegisterHandler(MsgType.Type, FuncName)
http://docs.unity3d.com/Manual/class-NetworkClient.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkClient.Connect.html 10
Feiner, COMS W4172, Spring 2018
NetworkIdentity
NetworkIdentity is Component that must be attached to all GameObjects that will interact over the network
Attaching it will delay the Start Function & enabling of the GameObjects and attached components until network communication is established
Set Server Only to prevent spawning on clients
http://docs.unity3d.com/Manual/class-NetworkIdentity.html11
NetworkIdentity
Local Player Authority: Allow client to own network objects instantiated for the client specifically
Required to allow clients to send “Commands” to server
AssetID: ID of Prefab used to create object
SceneID: ID of object in the scene (the GUID of scene objects)
NetworkID: ID of object in current network session
12http://docs.unity3d.com/Manual/class-NetworkIdentity.html
Feiner, COMS W4172, Spring 2018
NetworkBehaviour
NetworkBehaviours allow for special UNET-compatible functions and variables that can be executed over the network
Derives from MonoBehaviour
Objects with NetworkBehaviours will not start until spawned by a NetworkServer with NetworkServer.Spawn()
May also be spawned automatically by NetworkServer, if already present in scene
http://docs.unity3d.com/Manual/class-NetworkBehaviour.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.html 13
NetworkBehaviour
Synchronized variables for basic types
Tag variables with [SyncVar]
Network-based Callback Registration
OnStartServer, OnStartClient, OnSerialize, OnDeSerialize, etc.
{Client/Server}-specific functions
Allow you to specify methods that can be executed only on the server or client, with [Server] or [Client] tags, respectively
http://docs.unity3d.com/Manual/class-NetworkBehaviour.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.html 14
Feiner, COMS W4172, Spring 2018
NetworkBehaviour
RPC Calls to Client
Allow server to invoke methods on clients
Add [ClientRpc] tag
Add “Rpc” before method name
http://docs.unity3d.com/Manual/class-NetworkBehaviour.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.html 15
NetworkBehaviour
Client-to-Server Commands
Allow client to invoke methods on Server
Need Local Player Authority on attached NetworkIdentity
Add [Command] tag
Add “Cmd” before method name
http://docs.unity3d.com/Manual/class-NetworkBehaviour.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.html 16
Feiner, COMS W4172, Spring 2018
NetworkBehaviour
Network-based Events
Event system, powered by network messages, that can trigger other behaviors throughout project
Create delegate for your event
Declare member of that delegate
Add [SyncEvent] tag to event members
http://docs.unity3d.com/Manual/class-NetworkBehaviour.htmlhttp://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.html 17
NetworkTransform
Component allowing network synchronization of GameObject transformation components
As with standard NetworkBehaviour, optimized to send updates when object state is changed
Sync Options:
Transform, Rigidbody2D/3D
Change NetworkSendRate for more frequent updates (smoothing the transformation)
Set to 0 for objects that will not update (over network) after creation
http://docs.unity3d.com/Manual/class-NetworkTransform.html18
Feiner, COMS W4172, Spring 2018
NetworkTransform
Smooth translation on clients with InterpolateMovement
The higher the number, the faster the interpolation
Smooth rotation on clients with InterpolateRotation
The higher the number, the faster the interpolation
Set rotation axis with RotationAxis
Set CompressRotation to Low/High and SyncAngularVelocity, to further customize rotation behavior
http://docs.unity3d.com/Manual/class-NetworkTransform.html19
Thank You!Questions?
20