OnSerializeWrite
How do I use On Serialize Write
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
Line | Code | Ex.. |
---|---|---|
3200 | Hashtable evData = this.OnSerializeWrite(view); | 1 |
3273 | private Hashtable OnSerializeWrite(PhotonView view) | 2 |
File name: NetworkingPeer.cs
Copy
3147 public void RunViewUpdate()
3148 {
3149 if (!PhotonNetwork.connected || PhotonNetwork.offlineMode)
3150 {
3151 return;
3152 }
3153
3154 if (this.mActors == null ||
3155#if !PHOTON_DEVELOP
3156 this.mActors.Count <= 1
3157#endif
3158 )
3159 {
3160 return; // No need to send OnSerialize messages (these are never buffered anyway)
3161 }
3162
3163 dataPerGroupReliable.Clear();
3164 dataPerGroupUnreliable.Clear();
3165
3166 /* Format of the data hashtable:
3167 * Hasthable dataPergroup*
3168 * [(byte)0] = this.ServerTimeInMilliSeconds;
3169 * OPTIONAL: [(byte)1] = currentLevelPrefix;
3170 * + data
3171 */
3172
3173 foreach (KeyValuePair
3174 {
3175 PhotonView view = kvp.Value;
3176
3177 if (view.synchronization != ViewSynchronization.Off)
3178 {
3179 // Fetch all sending photonViews
3180 if (view.isMine)
3181 {
3182 #if UNITY_2_6_1 || UNITY_2_6 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
3183 if (!view.gameObject.active)
3184 {
3185 continue; // Only on actives
3186 }
3187 #else
3188 if (!view.gameObject.activeInHierarchy)
3189 {
3190 continue; // Only on actives
3191 }
3192 #endif
3193
3194 if (this.blockSendingGroups.Contains(view.group))
3195 {
3196 continue; // Block sending on this group
3197 }
3198
3199 // Run it trough its OnSerialize
3200 Hashtable evData = this.OnSerializeWrite(view);
3201 if (evData == null)
3202 {
3203 continue;
3204 }
3205
3206 if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed || view.mixedModeIsReliable)
3207 {
3208 if (!evData.ContainsKey((byte)1) && !evData.ContainsKey((byte)2))
3209 {
3210 // Everything has been removed by compression, nothing to send
3211 }
3212 else
3213 {
3214 if (!dataPerGroupReliable.ContainsKey(view.group))
3215 {
3216 dataPerGroupReliable[view.group] = new Hashtable();
3217 dataPerGroupReliable[view.group][(byte)0] = this.ServerTimeInMilliSeconds;
3218 if (currentLevelPrefix >= 0)
3219 {
3220 dataPerGroupReliable[view.group][(byte)1] = this.currentLevelPrefix;
3221 }
3222 }
3223 Hashtable groupHashtable = dataPerGroupReliable[view.group];
3224 groupHashtable.Add((short)groupHashtable.Count, evData);
3225 }
3226 }
3227 else
3228 {
3229 if (!dataPerGroupUnreliable.ContainsKey(view.group))
3230 {
3231 dataPerGroupUnreliable[view.group] = new Hashtable();
3232 dataPerGroupUnreliable[view.group][(byte)0] = this.ServerTimeInMilliSeconds;
3233 if (currentLevelPrefix >= 0)
3234 {
3235 dataPerGroupUnreliable[view.group][(byte)1] = this.currentLevelPrefix;
3236 }
3237 }
3238 Hashtable groupHashtable = dataPerGroupUnreliable[view.group];
3239 groupHashtable.Add((short)groupHashtable.Count, evData);
3240 }
3241 }
3242 else
3243 {
3244 // Debug.Log(" NO OBS on " + view.name + " " + view.owner);
3245 }
3246 }
3247 else
3248 {
3249 }
3250 }
3251
3252 //Send the messages: every group is send in it's own message and unreliable and reliable are split as well
3253 RaiseEventOptions options = new RaiseEventOptions();
3254
3255#if PHOTON_DEVELOP
3256 options.Receivers = ReceiverGroup.All;
3257#endif
3258
3259 foreach (KeyValuePair
3260 {
3261 options.InterestGroup = (byte)kvp.Key;
3262 this.OpRaiseEvent(PunEvent.SendSerializeReliable, kvp.Value, true, options);
3263 }
3264 foreach (KeyValuePair
3265 {
3266 options.InterestGroup = (byte)kvp.Key;
3267 this.OpRaiseEvent(PunEvent.SendSerialize, kvp.Value, false, options);
3268 }
3269 }
File name: NetworkingPeer.cs
Copy
3271 // calls OnPhotonSerializeView (through ExecuteOnSerialize)
3273 private Hashtable OnSerializeWrite(PhotonView view)
3274 {
3275 PhotonStream pStream = new PhotonStream( true, null );
3276 PhotonMessageInfo info = new PhotonMessageInfo( this.mLocalActor, this.ServerTimeInMilliSeconds, view );
3277
3278 // each view creates a list of values that should be sent
3279 view.SerializeView( pStream, info );
3280
3281 if( pStream.Count == 0 )
3282 {
3283 return null;
3284 }
3285
3286 object[] dataArray = pStream.data.ToArray();
3287
3288 if (view.synchronization == ViewSynchronization.UnreliableOnChange)
3289 {
3290 if (AlmostEquals(dataArray, view.lastOnSerializeDataSent))
3291 {
3292 if (view.mixedModeIsReliable)
3293 {
3294 return null;
3295 }
3296
3297 view.mixedModeIsReliable = true;
3298 view.lastOnSerializeDataSent = dataArray;
3299 }
3300 else
3301 {
3302 view.mixedModeIsReliable = false;
3303 view.lastOnSerializeDataSent = dataArray;
3304 }
3305 }
3306
3307 // EVDATA:
3308 // 0=View ID (an int, never compressed cause it's not in the data)
3309 // 1=data of observed type (different per type of observed object)
3310 // 2=compressed data (in this case, key 1 is empty)
3311 // 3=list of values that are actually null (if something was changed but actually IS null)
3312 Hashtable evData = new Hashtable();
3313 evData[(byte)0] = (int)view.viewID;
3314 evData[(byte)1] = dataArray; // this is the actual data (script or observed object)
3315
3316
3317 if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3318 {
3319 // compress content of data set (by comparing to view.lastOnSerializeDataSent)
3320 // the "original" dataArray is NOT modified by DeltaCompressionWrite
3321 // if something was compressed, the evData key 2 and 3 are used (see above)
3322 bool somethingLeftToSend = this.DeltaCompressionWrite(view, evData);
3323
3324 // buffer the full data set (for next compression)
3325 view.lastOnSerializeDataSent = dataArray;
3326
3327 if (!somethingLeftToSend)
3328 {
3329 return null;
3330 }
3331 }
3332
3333 return evData;
3334 }
OnSerializeWrite 136 lượt xem
Gõ tìm kiếm nhanh...