SerializeView
How do I use Serialize View
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PickupController.cs
Copy
286 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
287 {
288 if (stream.isWriting)
289 {
290 stream.SendNext(this.transform.position);
291 stream.SendNext((byte)this._characterState);
292 }
293 else
294 {
295 bool initialRemotePosition = (remotePosition == Vector3.zero);
296
297 remotePosition = (Vector3)stream.ReceiveNext();
298 this._characterState = (PickupCharacterState)((byte)stream.ReceiveNext());
299
300 if (initialRemotePosition)
301 {
302 // avoids lerping the character from "center" to the "current" position when this client joins
303 this.transform.position = remotePosition;
304 }
305 }
306 }
File name: CubeExtra.cs
Copy
21 // this method is called by PUN when this script is being "observed" by a PhotonView (setup in inspector)
22 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
23 {
24 // Always send transform (depending on reliability of the network view)
25 if (stream.isWriting)
26 {
27 Vector3 pos = transform.localPosition;
28 Quaternion rot = transform.localRotation;
29 stream.Serialize(ref pos);
30 stream.Serialize(ref rot);
31 }
32 // When receiving, buffer the information
33 else
34 {
35 // Receive latest state information
36 Vector3 pos = Vector3.zero;
37 Quaternion rot = Quaternion.identity;
38 stream.Serialize(ref pos);
39 stream.Serialize(ref rot);
40
41 lastMovement = (pos - latestCorrectPos) / (Time.time - lastTime);
42
43 lastTime = Time.time;
44 latestCorrectPos = pos;
45
46 transform.position = latestCorrectPos;
47 }
48 }
File name: CubeInter.cs
Copy
34 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
35 {
36 // Always send transform (depending on reliability of the network view)
37 if (stream.isWriting)
38 {
39 Vector3 pos = transform.localPosition;
40 Quaternion rot = transform.localRotation;
41 stream.Serialize(ref pos);
42 stream.Serialize(ref rot);
43 }
44 // When receiving, buffer the information
45 else
46 {
47 // Receive latest state information
48 Vector3 pos = Vector3.zero;
49 Quaternion rot = Quaternion.identity;
50 stream.Serialize(ref pos);
51 stream.Serialize(ref rot);
52
53 // Shift buffer contents, oldest data erased, 18 becomes 19, ... , 0 becomes 1
54 for (int i = m_BufferedState.Length - 1; i >= 1; i--)
55 {
56 m_BufferedState[i] = m_BufferedState[i - 1];
57 }
58
59
60 // Save currect received state as 0 in the buffer, safe to overwrite after shifting
61 State state;
62 state.timestamp = info.timestamp;
63 state.pos = pos;
64 state.rot = rot;
65 m_BufferedState[0] = state;
66
67 // Increment state count but never exceed buffer size
68 m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);
69
70 // Check integrity, lowest numbered state in the buffer is newest and so on
71 for (int i = 0; i < m_TimestampCount - 1; i++)
72 {
73 if (m_BufferedState[i].timestamp < m_BufferedState[i + 1].timestamp)
74 Debug.Log("State inconsistent");
75 }
76 }
77 }
File name: CubeLerp.cs
Copy
35 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
36 {
37 if (stream.isWriting)
38 {
39 Vector3 pos = transform.localPosition;
40 Quaternion rot = transform.localRotation;
41 stream.Serialize(ref pos);
42 stream.Serialize(ref rot);
43 }
44 else
45 {
46 // Receive latest state information
47 Vector3 pos = Vector3.zero;
48 Quaternion rot = Quaternion.identity;
49
50 stream.Serialize(ref pos);
51 stream.Serialize(ref rot);
52
53 latestCorrectPos = pos; // save this to move towards it in FixedUpdate()
54 onUpdatePos = transform.localPosition; // we interpolate from here to latestCorrectPos
55 fraction = 0; // reset the fraction we alreay moved. see Update()
56
57 transform.localRotation = rot; // this sample doesn't smooth rotation
58 }
59 }
File name: ThirdPersonNetwork.cs
Copy
31 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
32 {
33 if (stream.isWriting)
34 {
35 //We own this player: send the others our data
36 stream.SendNext((int)controllerScript._characterState);
37 stream.SendNext(transform.position);
38 stream.SendNext(transform.rotation);
39 }
40 else
41 {
42 //Network player, receive data
43 controllerScript._characterState = (CharacterState)(int)stream.ReceiveNext();
44 correctPlayerPos = (Vector3)stream.ReceiveNext();
45 correctPlayerRot = (Quaternion)stream.ReceiveNext();
46 }
47 }
File name: NetworkCharacter.cs
Copy
17 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
18 {
19 if (stream.isWriting)
20 {
21 // We own this player: send the others our data
22 stream.SendNext(transform.position);
23 stream.SendNext(transform.rotation);
24
25 myThirdPersonController myC = GetComponent
26 stream.SendNext((int)myC._characterState);
27 }
28 else
29 {
30 // Network player, receive data
31 this.correctPlayerPos = (Vector3)stream.ReceiveNext();
32 this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
33
34 myThirdPersonController myC = GetComponent
35 myC._characterState = (CharacterState)stream.ReceiveNext();
36 }
37 }
File name: PhotonConverter.cs
Copy
189 static void ConvertToPhotonAPI(string file)
190 {
191 string text = File.ReadAllText(file);
192
193 bool isJS = file.Contains(".js");
194
195 file = file.Replace("\\", "/"); // Get Class name for JS
196 string className = file.Substring(file.LastIndexOf("/")+1);
197 className = className.Substring(0, className.IndexOf("."));
198
199
200 //REGEXP STUFF
201 //Valid are: Space { } , /n /r
202 //string NOT_VAR = @"([^A-Za-z0-9_\[\]\.]+)";
203 string NOT_VAR_WITH_DOT = @"([^A-Za-z0-9_]+)";
204
205 //string VAR_NONARRAY = @"[^A-Za-z0-9_]";
206
207
208 //NetworkView
209 {
210 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkView" + NOT_VAR_WITH_DOT, "$1PhotonView$2");
211 text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
212 text = PregReplace(text, NOT_VAR_WITH_DOT + "stateSynchronization" + NOT_VAR_WITH_DOT, "$1synchronization$2");
213 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkStateSynchronization" + NOT_VAR_WITH_DOT, "$1ViewSynchronization$2"); // map Unity enum to ours
214 //.RPC
215 text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode.Server" + NOT_VAR_WITH_DOT, "$1PhotonTargets.MasterClient$2");
216 text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode" + NOT_VAR_WITH_DOT, "$1PhotonTargets$2");
217 }
218
219 //NetworkMessageInfo: 100%
220 {
221 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkMessageInfo" + NOT_VAR_WITH_DOT, "$1PhotonMessageInfo$2");
222 text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
223 }
224
225 //NetworkViewID:
226 {
227 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkViewID" + NOT_VAR_WITH_DOT, "$1int$2"); //We simply use an int
228 }
229
230 //NetworkPlayer
231 {
232 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPlayer" + NOT_VAR_WITH_DOT, "$1PhotonPlayer$2");
233 }
234
235 //Network
236 {
237 //Monobehaviour callbacks
238 {
239 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerConnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerConnected$2");
240 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerDisconnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerDisconnected$2");
241 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnNetworkInstantiate" + NOT_VAR_WITH_DOT, "$1OnPhotonInstantiate$2");
242 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnSerializeNetworkView" + NOT_VAR_WITH_DOT, "$1OnPhotonSerializeView$2");
243 text = PregReplace(text, NOT_VAR_WITH_DOT + "BitStream" + NOT_VAR_WITH_DOT, "$1PhotonStream$2");
244
245 //Not completely the same meaning
246 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnServerInitialized" + NOT_VAR_WITH_DOT, "$1OnCreatedRoom$2");
247 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnConnectedToServer" + NOT_VAR_WITH_DOT, "$1OnJoinedRoom$2");
248
249 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnectToMasterServer" + NOT_VAR_WITH_DOT, "$1OnFailedToConnectToPhoton$2");
250 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnect" + NOT_VAR_WITH_DOT, "$1OnFailedToConnect_OBSELETE$2");
251 }
252
253 //Variables
254 {
255
256 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.connections" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList$2");
257 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
258 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isClient" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
259
260 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPeerType" + NOT_VAR_WITH_DOT, "$1ConnectionState$2");
261 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.peerType" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.connectionState$2");
262 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Server" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
263 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Client" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
264 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.playerList.Length" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList.Count$2");
265
266 /*DROPPED:
267 minimumAllocatableViewIDs
268 natFacilitatorIP is dropped
269 natFacilitatorPort is dropped
270 connectionTesterIP
271 connectionTesterPort
272 proxyIP
273 proxyPort
274 useProxy
275 proxyPassword
276 */
277 }
278
279 //Methods
280 {
281 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.InitializeServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.CreateRoom$2");
282 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.Connect" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.JoinRoom$2");
283 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetAveragePing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
284 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetLastPing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
285 /*DROPPED:
286 TestConnection
287 TestConnectionNAT
288 HavePublicAddress
289 */
290 }
291
292 //Overall
293 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network" + NOT_VAR_WITH_DOT, "$1PhotonNetwork$2");
294
295
296 //Changed methods
297 string ignoreMe = @"([A-Za-z0-9_\[\]\(\) ]+)";
298
299 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.GetPing\\(" + ignoreMe+"\\);", "$1PhotonNetwork.GetPing();");
300 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.CloseConnection\\(" + ignoreMe+","+ignoreMe+"\\);", "$1PhotonNetwork.CloseConnection($2);");
301
302 }
303
304 //General
305 {
306 if (text.Contains("Photon")) //Only use the PhotonMonoBehaviour if we use photonView and friends.
307 {
308 if (isJS)//JS
309 {
310 if (text.Contains("extends MonoBehaviour"))
311 text = PregReplace(text, "extends MonoBehaviour", "extends Photon.MonoBehaviour");
312 else
313 text = "class " + className + " extends Photon.MonoBehaviour {\n" + text + "\n}";
314 }
315 else //C#
316 text = PregReplace(text, ": MonoBehaviour", ": Photon.MonoBehaviour");
317 }
318 }
319
320 File.WriteAllText(file, text);
321 }
File name: PhotonAnimatorView.cs
Copy
432 void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
433 {
434 if( m_Animator == null )
435 {
436 return;
437 }
438
439 if( stream.isWriting == true )
440 {
441 if( m_WasSynchronizeTypeChanged == true )
442 {
443 m_StreamQueue.Reset();
444 SerializeSynchronizationTypeState( stream );
445
446 m_WasSynchronizeTypeChanged = false;
447 }
448
449 m_StreamQueue.Serialize( stream );
450 SerializeDataDiscretly( stream );
451 }
452 else
453 {
454#if PHOTON_DEVELOP
455 if( ReceivingSender != null )
456 {
457 ReceivingSender.OnPhotonSerializeView( stream, info );
458 }
459 else
460#endif
461 {
462 if( stream.PeekNext() is byte[] )
463 {
464 DeserializeSynchronizationTypeState( stream );
465 }
466
467 m_StreamQueue.Deserialize( stream );
468 DeserializeDataDiscretly( stream );
469 }
470 }
471 }
File name: PhotonRigidbody2DView.cs
Copy
32 void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
33 {
34 if( stream.isWriting == true )
35 {
36 if( m_SynchronizeVelocity == true )
37 {
38 stream.SendNext( m_Body.velocity );
39 }
40
41 if( m_SynchronizeAngularVelocity == true )
42 {
43 stream.SendNext( m_Body.angularVelocity );
44 }
45 }
46 else
47 {
48 if( m_SynchronizeVelocity == true )
49 {
50 m_Body.velocity = (Vector2)stream.ReceiveNext();
51 }
52
53 if( m_SynchronizeAngularVelocity == true )
54 {
55 m_Body.angularVelocity = (float)stream.ReceiveNext();
56 }
57 }
58 }
File name: PhotonRigidbodyView.cs
Copy
32 void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
33 {
34 if( stream.isWriting == true )
35 {
36 if( m_SynchronizeVelocity == true )
37 {
38 stream.SendNext( m_Body.velocity );
39 }
40
41 if( m_SynchronizeAngularVelocity == true )
42 {
43 stream.SendNext( m_Body.angularVelocity );
44 }
45 }
46 else
47 {
48 if( m_SynchronizeVelocity == true )
49 {
50 m_Body.velocity = (Vector3)stream.ReceiveNext();
51 }
52
53 if( m_SynchronizeAngularVelocity == true )
54 {
55 m_Body.angularVelocity = (Vector3)stream.ReceiveNext();
56 }
57 }
58 }
SerializeView 127 lượt xem
Gõ tìm kiếm nhanh...