CacheProperties
How do I use Cache Properties
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: NetworkingPeer.cs
Copy
530 // joins a room and sets your current username as custom actorproperty (will broadcast that)
536 private void ReadoutProperties(Hashtable gameProperties, Hashtable pActorProperties, int targetActorNr)
537 {
538 // Debug.LogWarning("ReadoutProperties gameProperties: " + gameProperties.ToStringFull() + " pActorProperties: " + pActorProperties.ToStringFull() + " targetActorNr: " + targetActorNr);
539 // read game properties and cache them locally
540 if (this.mCurrentGame != null && gameProperties != null)
541 {
542 this.mCurrentGame.CacheProperties(gameProperties);
543 SendMonoMessage(PhotonNetworkingMessage.OnPhotonCustomRoomPropertiesChanged, gameProperties);
544 if (PhotonNetwork.automaticallySyncScene)
545 {
546 this.LoadLevelIfSynced(); // will load new scene if sceneName was changed
547 }
548 }
549
550 if (pActorProperties != null && pActorProperties.Count > 0)
551 {
552 if (targetActorNr > 0)
553 {
554 // we have a single entry in the pActorProperties with one
555 // user's name
556 // targets MUST exist before you set properties
557 PhotonPlayer target = this.GetPlayerWithID(targetActorNr);
558 if (target != null)
559 {
560 Hashtable props = this.GetActorPropertiesForActorNr(pActorProperties, targetActorNr);
561 target.InternalCacheProperties(props);
562 SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerPropertiesChanged, target, props);
563 }
564 }
565 else
566 {
567 // in this case, we've got a key-value pair per actor (each
568 // value is a hashtable with the actor's properties then)
569 int actorNr;
570 Hashtable props;
571 string newName;
572 PhotonPlayer target;
573
574 foreach (object key in pActorProperties.Keys)
575 {
576 actorNr = (int)key;
577 props = (Hashtable)pActorProperties[key];
578 newName = (string)props[ActorProperties.PlayerName];
579
580 target = this.GetPlayerWithID(actorNr);
581 if (target == null)
582 {
583 target = new PhotonPlayer(false, actorNr, newName);
584 this.AddNewPlayer(actorNr, target);
585 }
586
587 target.InternalCacheProperties(props);
588 SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerPropertiesChanged, target, props);
589 }
590 }
591 }
592 }
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
112 internal protected PhotonPlayer(bool isLocal, int actorID, Hashtable properties)
113 {
114 this.customProperties = new Hashtable();
115 this.isLocal = isLocal;
116 this.actorID = actorID;
117
118 this.InternalCacheProperties(properties);
119 }
File name: PhotonPlayer.cs
Copy
152 internal void InternalCacheProperties(Hashtable properties)
153 {
154 if (properties == null || properties.Count == 0 || this.customProperties.Equals(properties))
155 {
156 return;
157 }
158
159 if (properties.ContainsKey(ActorProperties.PlayerName))
160 {
161 this.nameField = (string)properties[ActorProperties.PlayerName];
162 }
163
164 this.customProperties.MergeStringKeys(properties);
165 this.customProperties.StripKeysWithNullValues();
166 }
File name: Room.cs
Copy
162 internal Room(string roomName, RoomOptions options) : base(roomName, null)
163 {
164 if (options == null)
165 {
166 options = new RoomOptions();
167 }
168
169 this.visibleField = options.isVisible;
170 this.openField = options.isOpen;
171 this.maxPlayersField = (byte)options.maxPlayers;
172 this.autoCleanUpField = false; // defaults to false, unless set to true when room gets created.
173
174 this.CacheProperties(options.customRoomProperties);
175 this.propertiesListedInLobby = options.customRoomPropertiesForLobby;
176 }
File name: RoomInfo.cs
Copy
135 protected internal RoomInfo(string roomName, Hashtable properties)
136 {
137 this.CacheProperties(properties);
138
139 this.nameField = roomName;
140 }
File name: RoomInfo.cs
Copy
176 protected internal void CacheProperties(Hashtable propertiesToCache)
177 {
178 if (propertiesToCache == null || propertiesToCache.Count == 0 || this.customPropertiesField.Equals(propertiesToCache))
179 {
180 return;
181 }
182
183 // check of this game was removed from the list. in that case, we don't
184 // need to read any further properties
185 // list updates will remove this game from the game listing
186 if (propertiesToCache.ContainsKey(GameProperties.Removed))
187 {
188 this.removedFromList = (Boolean)propertiesToCache[GameProperties.Removed];
189 if (this.removedFromList)
190 {
191 return;
192 }
193 }
194
195 // fetch the "well known" properties of the room, if available
196 if (propertiesToCache.ContainsKey(GameProperties.MaxPlayers))
197 {
198 this.maxPlayersField = (byte)propertiesToCache[GameProperties.MaxPlayers];
199 }
200
201 if (propertiesToCache.ContainsKey(GameProperties.IsOpen))
202 {
203 this.openField = (bool)propertiesToCache[GameProperties.IsOpen];
204 }
205
206 if (propertiesToCache.ContainsKey(GameProperties.IsVisible))
207 {
208 this.visibleField = (bool)propertiesToCache[GameProperties.IsVisible];
209 }
210
211 if (propertiesToCache.ContainsKey(GameProperties.PlayerCount))
212 {
213 this.playerCount = (int)((byte)propertiesToCache[GameProperties.PlayerCount]);
214 }
215
216 if (propertiesToCache.ContainsKey(GameProperties.CleanupCacheOnLeave))
217 {
218 this.autoCleanUpField = (bool)propertiesToCache[GameProperties.CleanupCacheOnLeave];
219 }
220
221 //if (propertiesToCache.ContainsKey(GameProperties.PropsListedInLobby))
222 //{
223 // // could be cached but isn't useful
224 //}
225
226 // merge the custom properties (from your application) to the cache (only string-typed keys will be kept)
227 this.customPropertiesField.MergeStringKeys(propertiesToCache);
228 }
CacheProperties 128 lượt xem
Gõ tìm kiếm nhanh...