OnSerialize
How do I use On Serialize
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PickupController.cs
Copy
73 // Are we jumping? (Initiated with jump button and not grounded yet)
77 // Are we moving backwards (This locks the camera to not do a 180 degree spin)
81 // When did the user start walking (Used for going into trot after a while)
87 // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
101 void Awake()
102 {
103 // PUN: automatically determine isControllable, if this GO has a PhotonView
104 PhotonView pv = this.gameObject.GetComponent
105 if (pv != null)
106 {
107 isControllable = pv.isMine;
108
109 // The pickup demo assigns this GameObject as the PhotonPlayer.TagObject. This way, we can access this character (controller, position, etc) easily
110 if (this.AssignAsTagObject)
111 {
112 pv.owner.TagObject = this.gameObject;
113 }
114
115 // please note: we change this setting on ANY PickupController if "DoRotate" is off. not only locally when it's "our" GameObject!
116 if (pv.observed is Transform && !DoRotate)
117 {
118 pv.onSerializeTransformOption = OnSerializeTransform.OnlyPosition;
119 }
120 }
121
122
123 moveDirection = transform.TransformDirection(Vector3.forward);
124
125 _animation = GetComponent
126 if (!_animation)
127 Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");
128
129 if (!idleAnimation)
130 {
131 _animation = null;
132 Debug.Log("No idle animation found. Turning off animations.");
133 }
134 if (!walkAnimation)
135 {
136 _animation = null;
137 Debug.Log("No walk animation found. Turning off animations.");
138 }
139 if (!runAnimation)
140 {
141 _animation = null;
142 Debug.Log("No run animation found. Turning off animations.");
143 }
144 if (!jumpPoseAnimation && canJump)
145 {
146 _animation = null;
147 Debug.Log("No jump animation found and the character has canJump enabled. Turning off animations.");
148 }
149 }
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: PhotonViewInspector.cs
Copy
145 void DrawSpecificTypeSerializationOptions()
146 {
147 if( m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Transform ) ).Count > 0 ||
148 ( m_Target.observed != null && m_Target.observed.GetType() == typeof( Transform ) ) )
149 {
150 m_Target.onSerializeTransformOption = (OnSerializeTransform)EditorGUILayout.EnumPopup( "Transform Serialization:", m_Target.onSerializeTransformOption );
151
152 }
153 else if( m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Rigidbody ) ).Count > 0 ||
154 ( m_Target.observed != null && m_Target.observed.GetType() == typeof( Rigidbody ) ) ||
155 m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Rigidbody2D ) ).Count > 0 ||
156 ( m_Target.observed != null && m_Target.observed.GetType() == typeof( Rigidbody2D ) ) )
157 {
158 m_Target.onSerializeRigidBodyOption = (OnSerializeRigidBody)EditorGUILayout.EnumPopup( "Rigidbody Serialization:", m_Target.onSerializeRigidBodyOption );
159 }
160 }
File name: PhotonViewInspector.cs
Copy
162 void DrawSpecificTypeOptions()
163 {
164 if( m_Target.observed != null )
165 {
166 Type type = m_Target.observed.GetType();
167 if( type == typeof( Transform ) )
168 {
169 m_Target.onSerializeTransformOption = (OnSerializeTransform)EditorGUILayout.EnumPopup( "Serialization:", m_Target.onSerializeTransformOption );
170
171 }
172 else if( type == typeof( Rigidbody ) )
173 {
174 m_Target.onSerializeRigidBodyOption = (OnSerializeRigidBody)EditorGUILayout.EnumPopup( "Serialization:", m_Target.onSerializeRigidBodyOption );
175
176 }
177 }
178 }
OnSerialize 220 lượt xem
Gõ tìm kiếm nhanh...