UNET HLAPI guide
In order to use any of the unity networking HLAPI features you must include UnityEngine.Networking and inherit from NetworkBehaviour.
UnityEngine.Networking;
1 | UnityEngine.Networking; |
To call Command or ClientRPC, the object has to have authority and prefix Cmd/Rpc as in examples below. Keep in mind that only one player can have authority over a single object.
1 | [ ] |
1 | [ ] |
The arguments passed to commands and ClientRpc calls are serialized and sent over the network. These arguments can:
1 | basic types (byte, int, float, string, UInt64, etc) |
1 | //These can be useful for function calls such as |
If you want to call a function only on one particular client you should use the [TargetRpc] attribute. By default it must have NetworkConnection argument. You can get the NetworkConnection to client by using NetworkIdentity.connectionToClient
1 | //can only get called from server |
1 | //channel variable sets the default channel |
1 | // you can define a SyncVar like this: |
You can mimic SyncVar behavior for these types of lists:
SyncListString, SyncListFloat, SyncListInt, SyncListUInt, SyncListBool
1 | SyncListBool DrunkList = new SyncListBool(); |
You can sync structs as well. Although they will be updated as a ‘whole’ instead of single variables inside them. Usage below:
1 | public struct Player |
If you want to sync an Instantiation of GameObject then Spawn is what you are searching for:
1 | //prefab reference |
There are helper functions to find networked objects that you know the NetworkInstanceId.
1 | //find object by their GetComponent<NetworkIdentity>().netId; |
Offline scene should be the connection scene where you have the host/connect.
Online scene is the scene that your players will transition to after starting.
- You can only add scenes that have been added to the build settings!
HLAPI components
NetworkManager – manages connecting, starting server and players in order to wire UI, you should wrap the calls in public void methods. Otherwise you can just use the NetworkManagerHUD component, just keep in mind you have almost no control over it.
1 | using UnityEngine; |
NetworkIdentity – all networked components should have this In the inspector it has two bool values Server Only -> this will make the object live only on the server (useful for manager, like spawning enemies) Local Player Authority -> gives authority to the local player, you can’t call commands unless this is selected
NetworkTransform – used for syncing transform. Any changes on the client side will not affect server unless local player authority is set The component has many issues and you should keep that in mind. When syncing physics you should move object using Physics, moving the transform will not sync position. Interpolation does not work as of right now(bug) You can have only one NetworkTransform per root game object To sync transorm of child objects use NetworkTransformChild NetworkTransformVisualizer – is supposed to visualise interpolation, but since interpolation is broken it doesn’t have any use right now.
NetworkAnimator – will sync the network animator states
NetworkIdentity – all networked components should have this In the inspector it has two bool values Server Only -> this will make the object live only on the server (useful for manager, like spawning enemies) Local Player Authority -> gives authority to the local player, you can’t call commands unless this is selected
NetworkTransform – used for syncing transform. Any changes on the client side will not affect server unless local player authority is set The component has many issues and you should keep that in mind. When syncing physics you should move object using Physics, moving the transform will not sync position. Interpolation does not work as of right now(bug) You can have only one NetworkTransform per root game object To sync transorm of child objects use NetworkTransformChild
NetworkTransformVisualizer – is supposed to visualise interpolation, but since interpolation is broken it doesn’t have any use right now.
You should also read the following documentation page on UNET concepts:
http://docs.unity3d.com/Manual/UNetConcepts.html
Qos channel explanations:
http://docs.unity3d.com/ScriptReference/Networking.QosType.html