Advertisement
Guest User

Networking with Delegates Example

a guest
Feb 1st, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public Class MyNetworkManager
  2. {
  3.     void Awake()
  4.     {
  5.         Networking.onData += OnDataRecieved; //Adding the custom thing to do on receiving data
  6.         Networking.onPlayerDisconnect += OnLeaveGame; //Adding the custom thing to do when a player leaves
  7.     }
  8.  
  9.     //The custom thing I want to do with data received
  10.     void OnDataRecieved (NetworkInfo Info, object[] Data)
  11.     {
  12.         //Info contains networking information such as who sent the data
  13.         byte OperationCode = Data [0];
  14.         byte UnitID = Data[1];
  15.         if (OperationCode == 0)
  16.         {
  17.             Vector2 Position = Data [2];
  18.             byte Health = Data [3];
  19.             GetUnit (UnitID).SetPosition (Position);
  20.             GetUnit (UnitId).SetHealth (Health);
  21.         }
  22.         if (OperationCode == 1)
  23.         {
  24.             GetUnit (UnitID).Explode ();
  25.         }
  26.     }
  27.  
  28.     //The custom thing I want to do when a player leaves a game
  29.     void OnLeaveGame (NetworkInfo)
  30.     {
  31.     object[] SendData = object [] {1, MyUnit.ID};
  32.     SendMessage (Receivers.All, SendData); //This triggers onData on receiving clients
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement