void RPCSetPlayerCustomString(uLink.NetworkPlayer player, string key, string data) { Debug.Log("in RPCSetPlayerCustomString: p=" + player.ToString() + " k=" + key); // Update the property in local memory SetPlayerCustomProperty(player, key, data); // If we're the server, this needs to be sent to all the other clients if (uLink.Network.isServer) { foreach (uLink.NetworkPlayer p in NetworkDirector.Players) { if (p != player && p != uLink.Network.player) { myNetworkView.RPC("RPCSetPlayerCustomString", p, key, data); } } } }
void RPCOnNetworkPlayerDisconnected(uLink.NetworkPlayer player) { // Remove the player's properties playerCustomProperties.Remove(player.ToString()); // Remove the player players.Remove(player); }
/// <summary> /// Called by a client or a server to broadcast their custom property to all other players /// </summary> /// <param name='key'> /// Key. /// </param> /// <param name='data'> /// Data. /// </param> void BroadcastCustomProperty(uLink.RPCMode target, uLink.NetworkPlayer owner, string key, object data) { Debug.Log("in BroadcastCustomProperty target = " + target + " owner = " + owner.ToString() + " key = " + key); if (data.GetType() == typeof(string)) { myNetworkView.RPC("RPCSetPlayerCustomString", target, uLink.Network.player, key, (string)data); } else if (data.GetType() == typeof(int)) { myNetworkView.RPC("RPCSetPlayerCustomInt", target, uLink.Network.player, key, (int)data); } else if (data.GetType() == typeof(float)) { myNetworkView.RPC("RPCSetPlayerCustomFloat", target, uLink.Network.player, key, (float)data); } else { Debug.LogError("Unsupported player property type:" + data.GetType().ToString()); } }
/// <summary> /// Instantiates a game object /// </summary> /// <param name="owner">The owning client</param> /// <param name="prefabName">Name of the prefab to instantiate.</param> /// <param name="position">Position Vector3 to apply on instantiation.</param> /// <param name="rotation">Rotation Quaternion to apply on instantiation.</param> /// <param name="group">The group for this view.</param> /// <param name="data">Optional instantiation data.</param> /// </param> public void InstantiateClientObject(uLink.NetworkPlayer owner, string prefabName, Vector3 position, Quaternion rotation, int group, params object[] data) { // Right now we only support this being called from the server, which is also the master client. if (isMasterClient) { // Have uLink instantiate the object Debug.Log(string.Format("in InstantiateClientObject of prefab {0} for owner {1}", prefabName, owner.ToString())); uLink.Network.Instantiate(owner, prefabName, position, rotation, group, data); } else { Debug.LogError("Called InstantiateClientObject from someone who isn't the master client!"); } }
/// <summary> /// Called by a server or a client to set the custom property for a player in the game. /// </summary> /// <param name='player'> /// The player who owns the proerty. /// </param> /// <param name='key'> /// Key. /// </param> /// <param name='data'> /// Data. /// </param> public static void SetPlayerCustomProperty(uLink.NetworkPlayer player, string key, object data) { if (!playerCustomProperties.ContainsKey(player.ToString())) { playerCustomProperties.Add(player.ToString(), new Hashtable()); } Hashtable ht = playerCustomProperties[player.ToString()]; if (!ht.ContainsKey(key)) { ht.Add(key, data); } else { ht[key] = data; } }
/// <summary> /// Called by a server or a client to get a custom player property. /// </summary> /// <returns> /// The custom player property. /// </returns> /// <param name='player'> /// Player. /// </param> /// <param name='key'> /// Key. /// </param> public static object GetCustomPlayerProperty(uLink.NetworkPlayer player, string key) { if (playerCustomProperties.ContainsKey(player.ToString())) { if (playerCustomProperties[player.ToString()].ContainsKey(key)) { return playerCustomProperties[player.ToString()][key]; } } return null; }