Stardog

Unity Unet Networking Notes

Aug 18th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. ----------------------------------------
  2. NETWORKING
  3. ----------------------------------------
  4.  
  5. (COMMANDS)
  6. http://docs.unity3d.com/Manual/UNetActions.html | http://docs.unity3d.com/Manual/class-NetworkBehaviour.html
  7. Commands are the way for clients to request to do something on the server.
  8. Called from client to server, or server to server. Function only runs on server's machine.
  9. Can only be run from your own player object (isLocalPlayer must be true?), unlike RPC's.
  10. Must be prefixed Cmd, e.g. CmdFunctionName().
  11. Parameters must be simple (int, float, Vector3, bool, struct, GameObject[1][2], NetworkIdentity[1][2], etc).
  12. http://answers.unity3d.com/questions/1001474/unet-parameter-restrictions-for-command-and-client.html
  13. [1] http://forum.unity3d.com/threads/networkserver-findlocalobject-networkinstanceid-netid.327114/#post-2121215
  14. [2] http://forum.unity3d.com/threads/how-do-i-synchronize-gameobjects-or-components.347241/#post-2247157
  15. Instantiating objects (that show for everyone) must be done by server using NetworkServer.Spawn. If client is trying to do it, it must use a Command. Instantiate as normal on the server, then call "NetworkServer.Spawn(objInstance)" to make it appear to all clients.
  16. (?) To send reference to GameObject as parameter, send its netId (NetworkInstanceID). Can be found from its NetworkIdentity component.
  17. http://forum.unity3d.com/threads/networkserver-findlocalobject-networkinstanceid-netid.327114/#post-2121215
  18. "Just pass the GameObject (or NetworkIdentity) as a parameter to the ClientRpc call and the system will look it up for you."
  19. Client cannot access an "unowned" network object to run functions on it via GetComponent. To run a function on another object, it must be done by the server. The client, at the point of wanting to access the unowned object, should instead run a Command to make the server access it.
  20. Example: Client has detected a button via raycast. It wants to use "buttonGO.GetComponent<Button>().Press()" on the button, but can't, even if it's a Command. Instead, replace it with "CmdPressButton(buttonGO.GetComponent<NetworkIdentity>().netId)"
  21. The code inside the CmdPressButton(NetworkInstanceId id) will contain "GameObject targetGO = NetworkServer.FindLocalObject(id);", and "targetGO.GetComponent<Button>().Press()".
  22. The server has now called the Press() function on its machine. If the client is hooked to any SyncVars/SyncEvents that change, it can run a hook function, but if not, the Press() ran by the server should call an RpcPress() function.
  23. Cannot return values (?)
  24.  
  25. (RPC)
  26. http://docs.unity3d.com/Manual/UNetActions.html | http://docs.unity3d.com/Manual/class-NetworkBehaviour.html
  27. Called from server to clients, function runs on every client.
  28. Called on "Hosts" also, because they are a local client.
  29. Must be prefixed Rpc, e.g. RpcFunctionName().
  30. Client RPC calls are not only invoked on player objects (like Commands are), they can be invoked on any NetworkIdentity object. (http://forum.unity3d.com/threads/unet-clientrpc-not-executing-on-host.320288/#post-2080713)
  31.  
  32. (SyncVar/SyncEvent/SyncList<T0>/hooks)
  33. (General)
  34. Can only be changed by server/host.
  35. Type must be simple (int, float, Vector3, bool, struct, GameObject[1], NetworkIdentity[1], etc)
  36. [1] http://forum.unity3d.com/threads/how-do-i-synchronize-gameobjects-or-components.347241/#post-2247157
  37. Server value only syncs/updates to clients if not using a hook.
  38. Creating a "syncedVar" version of your member "var" can be useful.
  39. (Hooks)
  40. Hook functions fire when synced var changes. [SyncVar(hook="OnVarChanged")]
  41. If using a hook, synced var must be set/updated inside the hook function.
  42. Hook functions receive the updated/new value as a parameter "OnVarChanged(int newValue)", but member variable remains old (so comparisons can be made). Member var must be forced to update like so: "memberVar = newValue".
  43. Hooks do not run on server, just host/clients. (hosts??)
  44. (SyncEvent)
  45. ...
  46. (SyncLists)
  47. http://docs.unity3d.com/ScriptReference/Networking.SyncList_1.html
  48. public SyncListInt ints = new SyncListInt();
  49. public SyncList<int> ints;
  50. https://www.reddit.com/r/Unity3D/comments/3bbk1y/help_with_unity_networking/
  51.  
  52.  
  53. (OTHER)
  54. OnStartServer should be used for safety.
  55. http://forum.unity3d.com/threads/gotchas-feedback-criticism.339705/
  56.  
  57. (SPAWNING)
  58. http://docs.unity3d.com/Manual/UNetSpawning.html
  59. A NetworkIdentity must be on the root game object of a spawnable prefab.
  60. NetworkBehaviour scripts must be on the same game object as the NetworkIdentity, not on child game objects.
  61. Prefabs can’t be registered with the NetworkManager unless they have a NetworkIdentity on their root object.
  62. Note that on the host, objects are not spawned for the local client as they already exist on the server. So no spawn handler functions will be called.
  63.  
  64. Instantiating/spawning structures have to be read from server's variable.
Add Comment
Please, Sign In to add comment