SetCustomProperties
How do I use Set Custom Properties
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: LoadbalancingPeer.cs
Copy
282 public bool OpSetCustomPropertiesOfActor(int actorNr, Hashtable actorProperties, bool broadcast, byte channelId)
283 {
284 return this.OpSetPropertiesOfActor(actorNr, actorProperties.StripToStringKeys(), broadcast, channelId);
285 }
File name: LoadbalancingPeer.cs
Copy
321 public bool OpSetCustomPropertiesOfRoom(Hashtable gameProperties, bool broadcast, byte channelId)
322 {
323 return this.OpSetPropertiesOfRoom(gameProperties.StripToStringKeys(), broadcast, channelId);
324 }
File name: NetworkingPeer.cs
Copy
3657 protected internal void SetLevelInPropsIfSynced(object levelId)
3658 {
3659 if (!PhotonNetwork.automaticallySyncScene || !PhotonNetwork.isMasterClient || PhotonNetwork.room == null)
3660 {
3661 return;
3662 }
3663 if (levelId == null)
3664 {
3665 Debug.LogError("Parameter levelId can't be null!");
3666 return;
3667 }
3668
3669 // check if "current level" is already set in props
3670 if (PhotonNetwork.room.customProperties.ContainsKey(NetworkingPeer.CurrentSceneProperty))
3671 {
3672 object levelIdInProps = PhotonNetwork.room.customProperties[NetworkingPeer.CurrentSceneProperty];
3673 if (levelIdInProps is int && Application.loadedLevel == (int)levelIdInProps)
3674 {
3675 return;
3676 }
3677 if (levelIdInProps is string && Application.loadedLevelName.Equals((string)levelIdInProps))
3678 {
3679 return;
3680 }
3681 }
3682
3683 // current level is not yet in props, so this client has to set it
3684 Hashtable setScene = new Hashtable();
3685 if (levelId is int) setScene[NetworkingPeer.CurrentSceneProperty] = (int)levelId;
3686 else if (levelId is string) setScene[NetworkingPeer.CurrentSceneProperty] = (string)levelId;
3687 else Debug.LogError("Parameter levelId must be int or string!");
3688
3689 PhotonNetwork.room.SetCustomProperties(setScene);
3690 this.SendOutgoingCommands(); // send immediately! because: in most cases the client will begin to load and not send for a while
3691 }
File name: PhotonNetwork.cs
Copy
1894 public static void SetPlayerCustomProperties(Hashtable customProperties)
1895 {
1896 if (customProperties == null)
1897 {
1898 customProperties = new Hashtable();
1899 foreach (object k in player.customProperties.Keys)
1900 {
1901 customProperties[(string)k] = null;
1902 }
1903 }
1904
1905 if (room != null && room.isLocalClientInside)
1906 {
1907 player.SetCustomProperties(customProperties);
1908 }
1909 else
1910 {
1911 player.InternalCacheProperties(customProperties);
1912 }
1913 }
File name: PhotonPlayer.cs
Copy
184 public void SetCustomProperties(Hashtable propertiesToSet)
185 {
186 if (propertiesToSet == null)
187 {
188 return;
189 }
190
191 // merge (delete null-values)
192 this.customProperties.MergeStringKeys(propertiesToSet); // includes a Equals check (simplifying things)
193 this.customProperties.StripKeysWithNullValues();
194
195 // send (sync) these new values
196 Hashtable customProps = propertiesToSet.StripToStringKeys() as Hashtable;
197 if (this.actorID > 0 && !PhotonNetwork.offlineMode)
198 {
199 PhotonNetwork.networkingPeer.OpSetCustomPropertiesOfActor(this.actorID, customProps, true, 0);
200 }
201 NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerPropertiesChanged, this, propertiesToSet);
202 }
File name: Room.cs
Copy
196 public void SetCustomProperties(Hashtable propertiesToSet)
197 {
198 if (propertiesToSet == null)
199 {
200 return;
201 }
202
203 // merge (delete null-values)
204 this.customProperties.MergeStringKeys(propertiesToSet); // includes a Equals check (simplifying things)
205 this.customProperties.StripKeysWithNullValues();
206
207
208 // send (sync) these new values
209 Hashtable customProps = propertiesToSet.StripToStringKeys() as Hashtable;
210 if (!PhotonNetwork.offlineMode)
211 {
212 PhotonNetwork.networkingPeer.OpSetCustomPropertiesOfRoom(customProps, true, 0);
213 }
214 NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnPhotonCustomRoomPropertiesChanged, propertiesToSet);
215 }
File name: InRoomRoundTimer.cs
Copy
28 private void StartRoundNow()
29 {
30 // in some cases, when you enter a room, the server time is not available immediately.
31 // time should be 0.0f but to make sure we detect it correctly, check for a very low value.
32 if (PhotonNetwork.time < 0.0001f)
33 {
34 // we can only start the round when the time is available. let's check that in Update()
35 startRoundWhenTimeIsSynced = true;
36 return;
37 }
38 startRoundWhenTimeIsSynced = false;
39
40
41
42 ExitGames.Client.Photon.Hashtable startTimeProp = new Hashtable(); // only use ExitGames.Client.Photon.Hashtable for Photon
43 startTimeProp[StartTimeKey] = PhotonNetwork.time;
44 PhotonNetwork.room.SetCustomProperties(startTimeProp); // implement OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged) to get this change everywhere
45 }
File name: PunPlayerScores.cs
Copy
15 public static void SetScore(this PhotonPlayer player, int newScore)
16 {
17 Hashtable score = new Hashtable(); // using PUN's implementation of Hashtable
18 score[PunPlayerScores.PlayerScoreProp] = newScore;
19
20 player.SetCustomProperties(score); // this locally sets the score and will sync it in-game asap.
21 }
File name: PunPlayerScores.cs
Copy
23 public static void AddScore(this PhotonPlayer player, int scoreToAddToCurrent)
24 {
25 int current = player.GetScore();
26 current = current + scoreToAddToCurrent;
27
28 Hashtable score = new Hashtable(); // using PUN's implementation of Hashtable
29 score[PunPlayerScores.PlayerScoreProp] = current;
30
31 player.SetCustomProperties(score); // this locally sets the score and will sync it in-game asap.
32 }
File name: PunTeams.cs
Copy
96 public static void SetTeam(this PhotonPlayer player, PunTeams.Team team)
97 {
98 if (!PhotonNetwork.connectedAndReady)
99 {
100 Debug.LogWarning("JoinTeam was called in state: " + PhotonNetwork.connectionStateDetailed + ". Not connectedAndReady.");
101 }
102
103 PunTeams.Team currentTeam = PhotonNetwork.player.GetTeam();
104 if (currentTeam != team)
105 {
106 PhotonNetwork.player.SetCustomProperties(new Hashtable() {{PunTeams.TeamPlayerProp, (byte) team}});
107 }
108 }
SetCustomProperties 161 lượt xem
Gõ tìm kiếm nhanh...