Receive
How do I use Receive
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: GameManager.cs
Copy
27 void OnGUI() {
28 GUI.skin = layout;
29 GUI.Label (new Rect (Screen.width / 2 - 150 - 12, 20, 100, 100), "" + PlayerScore1);
30 GUI.Label (new Rect (Screen.width / 2 + 150 + 12, 20, 100, 100), "" + PlayerScore2);
31
32 if (GUI.Button (new Rect (Screen.width / 2 - 60, 35, 120, 53), "RESTART")) {
33 PlayerScore1 = 0;
34 PlayerScore2 = 0;
35 theBall.SendMessage ("RestartGame", 0.5f, SendMessageOptions.RequireReceiver);
36 }
37
38 if (PlayerScore1 == 10) {
39 GUI.Label (new Rect (Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER ONE WINS");
40 theBall.SendMessage ("ResetBall", null, SendMessageOptions.RequireReceiver);
41 } else if (PlayerScore2 == 10) {
42 GUI.Label (new Rect (Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER TWO WINS");
43 theBall.SendMessage ("ResetBall", null, SendMessageOptions.RequireReceiver);
44 }
45 }
File name: SideWalls.cs
Copy
7 void OnTriggerEnter2D(Collider2D hitInfo) {
8 if (hitInfo.name == "Ball")
9 {
10 string wallName = transform.name;
11 GameManager.Score (wallName);
12 hitInfo.gameObject.SendMessage ("RestartGame", 1, SendMessageOptions.RequireReceiver);
13 }
14 }
File name: HubGui.cs
Copy
28 void OnGUI()
29 {
30 GUI.skin = this.Skin;
31 GUILayout.Space(10);
32
33 GUILayout.BeginHorizontal();
34 GUILayout.Space(10);
35 scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37 GUILayout.Label("Basics", m_Headline);
38 if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39 {
40 demoDescription = "
41 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42 }
43 if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44 {
45 demoDescription = "
46 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47 }
48 if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49 {
50 demoDescription = "
51 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52 }
53
54 GUILayout.Label("Advanced", m_Headline);
55 if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56 {
57 demoDescription = "
58 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59 this.webLink = new DemoBtn();
60 }
61 if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62 {
63 demoDescription = "
64 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65 this.webLink = new DemoBtn();
66 }
67
68 GUILayout.Label("Feature Demos", m_Headline);
69 if (GUILayout.Button("Chat", GUILayout.Width(280)))
70 {
71 demoDescription = "
72 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73 this.webLink = new DemoBtn();
74 }
75 if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76 {
77 demoDescription = "
78 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79 this.webLink = new DemoBtn();
80 }
81 if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82 {
83 demoDescription = "
84 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85 this.webLink = new DemoBtn();
86 }
87 if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88 {
89 demoDescription = "
90 this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91 this.webLink = new DemoBtn();
92 }
93 if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94 {
95 demoDescription = "
96 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97 this.webLink = new DemoBtn();
98 }
99
100 GUILayout.Label("Tutorial", m_Headline);
101 if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102 {
103 demoDescription = "
104 this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105 this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106 }
107 GUILayout.EndScrollView();
108
109 GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110 GUILayout.Label(demoDescription);
111 GUILayout.Space(10);
112 if (!string.IsNullOrEmpty(this.demoBtn.Text))
113 {
114 if (GUILayout.Button(this.demoBtn.Text))
115 {
116 Application.LoadLevel(this.demoBtn.Link);
117 }
118 }
119 if (!string.IsNullOrEmpty(this.webLink.Text))
120 {
121 if (GUILayout.Button(this.webLink.Text))
122 {
123 Application.OpenURL(this.webLink.Link);
124 }
125 }
126 GUILayout.EndVertical();
127
128
129 GUILayout.EndHorizontal();
130 }
File name: DemoMecanimGUI.cs
Copy
61 public void OnGUI()
62 {
63 GUI.skin = Skin;
64
65 string[] synchronizeTypeContent = new string[] { "Disabled", "Discrete", "Continuous" };
66
67 GUILayout.BeginArea( new Rect( Screen.width - 200 * m_FoundPlayerSlideIn - 400 * m_SlideIn, 0, 600, Screen.height ), GUI.skin.box );
68 {
69 GUILayout.Label( "Mecanim Demo", GUI.skin.customStyles[ 0 ] );
70
71 GUI.color = Color.white;
72 string label = "Settings";
73
74 if( m_IsOpen == true )
75 {
76 label = "Close";
77 }
78
79 if( GUILayout.Button( label, GUILayout.Width( 110 ) ) )
80 {
81 m_IsOpen = !m_IsOpen;
82 }
83
84 string parameters = "";
85
86 if( m_AnimatorView != null )
87 {
88 parameters += "Send Values:\n";
89
90 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
91 {
92 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
93
94 try
95 {
96 switch( parameter.Type )
97 {
98 case PhotonAnimatorView.ParameterType.Bool:
99 parameters += parameter.Name + " (" + ( m_AnimatorView.GetComponent
100 break;
101 case PhotonAnimatorView.ParameterType.Int:
102 parameters += parameter.Name + " (" + m_AnimatorView.GetComponent
103 break;
104 case PhotonAnimatorView.ParameterType.Float:
105 parameters += parameter.Name + " (" + m_AnimatorView.GetComponent
106 break;
107 }
108 }
109 catch
110 {
111 Debug.Log( "derrrr for " + parameter.Name );
112 }
113 }
114 }
115
116 if( m_RemoteAnimator != null )
117 {
118 parameters += "\nReceived Values:\n";
119
120 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
121 {
122 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
123
124 try
125 {
126 switch( parameter.Type )
127 {
128 case PhotonAnimatorView.ParameterType.Bool:
129 parameters += parameter.Name + " (" + ( m_RemoteAnimator.GetBool( parameter.Name ) ? "True" : "False" ) + ")\n";
130 break;
131 case PhotonAnimatorView.ParameterType.Int:
132 parameters += parameter.Name + " (" + m_RemoteAnimator.GetInteger( parameter.Name ) + ")\n";
133 break;
134 case PhotonAnimatorView.ParameterType.Float:
135 parameters += parameter.Name + " (" + m_RemoteAnimator.GetFloat( parameter.Name ).ToString( "0.00" ) + ")\n";
136 break;
137 }
138 }
139 catch
140 {
141 Debug.Log( "derrrr for " + parameter.Name );
142 }
143 }
144 }
145
146 GUIStyle style = new GUIStyle( GUI.skin.label );
147 style.alignment = TextAnchor.UpperLeft;
148
149 GUI.color = new Color( 1f, 1f, 1f, 1 - m_SlideIn );
150 GUI.Label( new Rect( 10, 100, 600, Screen.height ), parameters, style );
151
152 if( m_AnimatorView != null )
153 {
154 GUI.color = new Color( 1f, 1f, 1f, m_SlideIn );
155
156 GUILayout.Space( 20 );
157 GUILayout.Label( "Synchronize Parameters" );
158
159 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
160 {
161 GUILayout.BeginHorizontal();
162 {
163 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
164
165 GUILayout.Label( parameter.Name, GUILayout.Width( 100 ), GUILayout.Height( 36 ) );
166
167 int selectedValue = (int)parameter.SynchronizeType;
168 int newValue = GUILayout.Toolbar( selectedValue, synchronizeTypeContent );
169
170 if( newValue != selectedValue )
171 {
172 m_AnimatorView.SetParameterSynchronized( parameter.Name, parameter.Type, (PhotonAnimatorView.SynchronizeType)newValue );
173 }
174 }
175 GUILayout.EndHorizontal();
176 }
177 }
178 }
179 GUILayout.EndArea();
180 }
File name: PickupController.cs
Copy
151 void Update()
152 {
153 if (isControllable)
154 {
155 if (Input.GetButtonDown("Jump"))
156 {
157 lastJumpButtonTime = Time.time;
158 }
159
160 UpdateSmoothedMovementDirection();
161
162 // Apply gravity
163 // - extra power jump modifies gravity
164 // - controlledDescent mode modifies gravity
165 ApplyGravity();
166
167 // Apply jumping logic
168 ApplyJumping();
169
170
171 // Calculate actual motion
172 Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
173 movement *= Time.deltaTime;
174
175 //Debug.Log(movement.x.ToString("0.000") + ":" + movement.z.ToString("0.000"));
176
177 // Move the controller
178 CharacterController controller = GetComponent
179 collisionFlags = controller.Move(movement);
180
181 }
182
183 // PUN: if a remote position is known, we smooth-move to it (being late(r) but smoother)
184 if (this.remotePosition != Vector3.zero)
185 {
186 transform.position = Vector3.Lerp(transform.position, this.remotePosition, Time.deltaTime * this.RemoteSmoothing);
187 }
188
189 velocity = (transform.position - lastPos)*25;
190
191 // ANIMATION sector
192 if (_animation)
193 {
194 if (_characterState == PickupCharacterState.Jumping)
195 {
196 if (!jumpingReachedApex)
197 {
198 _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
199 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
200 _animation.CrossFade(jumpPoseAnimation.name);
201 }
202 else
203 {
204 _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
205 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
206 _animation.CrossFade(jumpPoseAnimation.name);
207 }
208 }
209 else
210 {
211 if (_characterState == PickupCharacterState.Idle)
212 {
213 _animation.CrossFade(idleAnimation.name);
214 }
215 else if (_characterState == PickupCharacterState.Running)
216 {
217 _animation[runAnimation.name].speed = runMaxAnimationSpeed;
218 if (this.isControllable)
219 {
220 _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
221 }
222 _animation.CrossFade(runAnimation.name);
223 }
224 else if (_characterState == PickupCharacterState.Trotting)
225 {
226 _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
227 if (this.isControllable)
228 {
229 _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
230 }
231 _animation.CrossFade(walkAnimation.name);
232 }
233 else if (_characterState == PickupCharacterState.Walking)
234 {
235 _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
236 if (this.isControllable)
237 {
238 _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
239 }
240 _animation.CrossFade(walkAnimation.name);
241 }
242
243 if (_characterState != PickupCharacterState.Running)
244 {
245 _animation[runAnimation.name].time = 0.0f;
246 }
247 }
248 }
249 // ANIMATION sector
250
251 // Set rotation to the move direction
252 if (IsGrounded())
253 {
254 // a specialty of this controller: you can disable rotation!
255 if (DoRotate)
256 {
257 transform.rotation = Quaternion.LookRotation(moveDirection);
258 }
259 }
260 else
261 {
262 /* This causes choppy behaviour when colliding with SIDES
263 * Vector3 xzMove = velocity;
264 xzMove.y = 0;
265 if (xzMove.sqrMagnitude > 0.001f)
266 {
267 transform.rotation = Quaternion.LookRotation(xzMove);
268 }*/
269 }
270
271 // We are in jump mode but just became grounded
272 if (IsGrounded())
273 {
274 lastGroundedTime = Time.time;
275 inAirVelocity = Vector3.zero;
276 if (jumping)
277 {
278 jumping = false;
279 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
280 }
281 }
282
283 lastPos = transform.position;
284 }
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: ThirdPersonController.cs
Copy
292 void Update()
293 {
294 if (isControllable)
295 {
296 if (Input.GetButtonDown("Jump"))
297 {
298 lastJumpButtonTime = Time.time;
299 }
300
301 UpdateSmoothedMovementDirection();
302
303 // Apply gravity
304 // - extra power jump modifies gravity
305 // - controlledDescent mode modifies gravity
306 ApplyGravity();
307
308 // Apply jumping logic
309 ApplyJumping();
310
311
312 // Calculate actual motion
313 Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
314 movement *= Time.deltaTime;
315
316 // Move the controller
317 CharacterController controller = GetComponent
318 collisionFlags = controller.Move(movement);
319 }
320 velocity = (transform.position - lastPos)*25;
321
322 // ANIMATION sector
323 if (_animation)
324 {
325 if (_characterState == CharacterState.Jumping)
326 {
327 if (!jumpingReachedApex)
328 {
329 _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
330 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
331 _animation.CrossFade(jumpPoseAnimation.name);
332 }
333 else
334 {
335 _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
336 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
337 _animation.CrossFade(jumpPoseAnimation.name);
338 }
339 }
340 else
341 {
342 if (this.isControllable && velocity.sqrMagnitude < 0.001f)
343 {
344 _characterState = CharacterState.Idle;
345 _animation.CrossFade(idleAnimation.name);
346 }
347 else
348 {
349 if (_characterState == CharacterState.Idle)
350 {
351 _animation.CrossFade(idleAnimation.name);
352 }
353 else if (_characterState == CharacterState.Running)
354 {
355 _animation[runAnimation.name].speed = runMaxAnimationSpeed;
356 if (this.isControllable)
357 {
358 _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
359 }
360 _animation.CrossFade(runAnimation.name);
361 }
362 else if (_characterState == CharacterState.Trotting)
363 {
364 _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
365 if (this.isControllable)
366 {
367 _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
368 }
369 _animation.CrossFade(walkAnimation.name);
370 }
371 else if (_characterState == CharacterState.Walking)
372 {
373 _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
374 if (this.isControllable)
375 {
376 _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
377 }
378 _animation.CrossFade(walkAnimation.name);
379 }
380
381 }
382 }
383 }
384 // ANIMATION sector
385
386 // Set rotation to the move direction
387 if (IsGrounded())
388 {
389
390 transform.rotation = Quaternion.LookRotation(moveDirection);
391
392 }
393 else
394 {
395 /* This causes choppy behaviour when colliding with SIDES
396 * Vector3 xzMove = velocity;
397 xzMove.y = 0;
398 if (xzMove.sqrMagnitude > 0.001f)
399 {
400 transform.rotation = Quaternion.LookRotation(xzMove);
401 }*/
402 }
403
404 // We are in jump mode but just became grounded
405 if (IsGrounded())
406 {
407 lastGroundedTime = Time.time;
408 inAirVelocity = Vector3.zero;
409 if (jumping)
410 {
411 jumping = false;
412 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
413 }
414 }
415
416 lastPos = transform.position;
417 }
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: PhotonAnimatorView.cs
Copy
275 void DeserializeDataContinuously()
276 {
277 if( m_StreamQueue.HasQueuedObjects() == false )
278 {
279 return;
280 }
281
282 for( int i = 0; i < m_SynchronizeLayers.Count; ++i )
283 {
284 if( m_SynchronizeLayers[ i ].SynchronizeType == SynchronizeType.Continuous )
285 {
286 m_Animator.SetLayerWeight( m_SynchronizeLayers[ i ].LayerIndex, (float)m_StreamQueue.ReceiveNext() );
287 }
288 }
289
290 for( int i = 0; i < m_SynchronizeParameters.Count; ++i )
291 {
292 SynchronizedParameter parameter = m_SynchronizeParameters[ i ];
293
294 if( parameter.SynchronizeType == SynchronizeType.Continuous )
295 {
296 switch( parameter.Type )
297 {
298 case ParameterType.Bool:
299 m_Animator.SetBool( parameter.Name, (bool)m_StreamQueue.ReceiveNext() );
300 break;
301 case ParameterType.Float:
302 m_Animator.SetFloat( parameter.Name, (float)m_StreamQueue.ReceiveNext() );
303 break;
304 case ParameterType.Int:
305 m_Animator.SetInteger( parameter.Name, (int)m_StreamQueue.ReceiveNext() );
306 break;
307 case ParameterType.Trigger:
308
309 break;
310 }
311 }
312 }
313 }
Download file with original file name:Receive
Receive 108 lượt xem
Gõ tìm kiếm nhanh...