SendMessage
How do I use Send Message
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: OnClickCallMethod.cs
Copy
11 public void OnClick()
12 {
13 if (this.TargetGameObject == null || string.IsNullOrEmpty(this.TargetMethod))
14 {
15 Debug.LogWarning(this + " can't call, cause GO or Method are empty.");
16 return;
17 }
18
19 this.TargetGameObject.SendMessage(this.TargetMethod);
20 }
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: 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: NetworkingPeer.cs
Copy
1937 public static void SendMonoMessage(PhotonNetworkingMessage methodString, params object[] parameters)
1938 {
1939 HashSet
1940 if (PhotonNetwork.SendMonoMessageTargets != null)
1941 {
1942 objectsToCall = PhotonNetwork.SendMonoMessageTargets;
1943 }
1944 else
1945 {
1946 objectsToCall = PhotonNetwork.FindGameObjectsWithComponent(PhotonNetwork.SendMonoMessageTargetType);
1947 }
1948
1949 string methodName = methodString.ToString();
1950 object callParameter = (parameters != null && parameters.Length == 1) ? parameters[0] : parameters;
1951 foreach (GameObject gameObject in objectsToCall)
1952 {
1953 gameObject.SendMessage(methodName, callParameter, SendMessageOptions.DontRequireReceiver);
1954 }
1955 }
File name: NetworkingPeer.cs
Copy
2279 internal GameObject DoInstantiate(Hashtable evData, PhotonPlayer photonPlayer, GameObject resourceGameObject)
2280 {
2281 // some values always present:
2282 string prefabName = (string)evData[(byte)0];
2283 int serverTime = (int)evData[(byte)6];
2284 int instantiationId = (int)evData[(byte)7];
2285
2286 Vector3 position;
2287 if (evData.ContainsKey((byte)1))
2288 {
2289 position = (Vector3)evData[(byte)1];
2290 }
2291 else
2292 {
2293 position = Vector3.zero;
2294 }
2295
2296 Quaternion rotation = Quaternion.identity;
2297 if (evData.ContainsKey((byte)2))
2298 {
2299 rotation = (Quaternion)evData[(byte)2];
2300 }
2301
2302 int group = 0;
2303 if (evData.ContainsKey((byte)3))
2304 {
2305 group = (int)evData[(byte)3];
2306 }
2307
2308 short objLevelPrefix = 0;
2309 if (evData.ContainsKey((byte)8))
2310 {
2311 objLevelPrefix = (short)evData[(byte)8];
2312 }
2313
2314 int[] viewsIDs;
2315 if (evData.ContainsKey((byte)4))
2316 {
2317 viewsIDs = (int[])evData[(byte)4];
2318 }
2319 else
2320 {
2321 viewsIDs = new int[1] { instantiationId };
2322 }
2323
2324 object[] incomingInstantiationData;
2325 if (evData.ContainsKey((byte)5))
2326 {
2327 incomingInstantiationData = (object[])evData[(byte)5];
2328 }
2329 else
2330 {
2331 incomingInstantiationData = null;
2332 }
2333
2334 // SetReceiving filtering
2335 if (group != 0 && !this.allowedReceivingGroups.Contains(group))
2336 {
2337 return null; // Ignore group
2338 }
2339
2340 // load prefab, if it wasn't loaded before (calling methods might do this)
2341 if (resourceGameObject == null)
2342 {
2343 if (!NetworkingPeer.UsePrefabCache || !NetworkingPeer.PrefabCache.TryGetValue(prefabName, out resourceGameObject))
2344 {
2345 resourceGameObject = (GameObject)Resources.Load(prefabName, typeof(GameObject));
2346 if (NetworkingPeer.UsePrefabCache)
2347 {
2348 NetworkingPeer.PrefabCache.Add(prefabName, resourceGameObject);
2349 }
2350 }
2351
2352 if (resourceGameObject == null)
2353 {
2354 Debug.LogError("PhotonNetwork error: Could not Instantiate the prefab [" + prefabName + "]. Please verify you have this gameobject in a Resources folder.");
2355 return null;
2356 }
2357 }
2358
2359 // now modify the loaded "blueprint" object before it becomes a part of the scene (by instantiating it)
2360 PhotonView[] resourcePVs = resourceGameObject.GetPhotonViewsInChildren();
2361 if (resourcePVs.Length != viewsIDs.Length)
2362 {
2363 throw new Exception("Error in Instantiation! The resource's PhotonView count is not the same as in incoming data.");
2364 }
2365
2366 for (int i = 0; i < viewsIDs.Length; i++)
2367 {
2368 // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
2369 // so we only set the viewID and instantiationId now. the instantiationData can be fetched
2370 resourcePVs[i].viewID = viewsIDs[i];
2371 resourcePVs[i].prefix = objLevelPrefix;
2372 resourcePVs[i].instantiationId = instantiationId;
2373 resourcePVs[i].isRuntimeInstantiated = true;
2374 }
2375
2376 this.StoreInstantiationData(instantiationId, incomingInstantiationData);
2377
2378 // load the resource and set it's values before instantiating it:
2379 GameObject go = (GameObject)GameObject.Instantiate(resourceGameObject, position, rotation);
2380
2381 for (int i = 0; i < viewsIDs.Length; i++)
2382 {
2383 // NOTE instantiating the loaded resource will keep the viewID but would not copy instantiation data, so it's set below
2384 // so we only set the viewID and instantiationId now. the instantiationData can be fetched
2385 resourcePVs[i].viewID = 0;
2386 resourcePVs[i].prefix = -1;
2387 resourcePVs[i].prefixBackup = -1;
2388 resourcePVs[i].instantiationId = -1;
2389 resourcePVs[i].isRuntimeInstantiated = false;
2390 }
2391
2392 this.RemoveInstantiationData(instantiationId);
2393
2394 // Send OnPhotonInstantiate callback to newly created GO.
2395 // GO will be enabled when instantiated from Prefab and it does not matter if the script is enabled or disabled.
2396 go.SendMessage(PhotonNetworkingMessage.OnPhotonInstantiate.ToString(), new PhotonMessageInfo(photonPlayer, serverTime, null), SendMessageOptions.DontRequireReceiver);
2397 return go;
2398 }
File name: InputToEvent.cs
Copy
32 void Update()
33 {
34 if( DetectPointedAtGameObject )
35 {
36 goPointedAt = RaycastObject( Input.mousePosition );
37 }
38
39 if( Input.touchCount > 0 )
40 {
41 Touch touch = Input.GetTouch( 0 );
42 this.currentPos = touch.position;
43
44 if( touch.phase == TouchPhase.Began )
45 {
46 Press( touch.position );
47 }
48 else if( touch.phase == TouchPhase.Ended )
49 {
50 Release( touch.position );
51 }
52
53 return;
54 }
55
56 currentPos = Input.mousePosition;
57 if( Input.GetMouseButtonDown( 0 ) )
58 {
59 Press( Input.mousePosition );
60 }
61 if( Input.GetMouseButtonUp( 0 ) )
62 {
63 Release( Input.mousePosition );
64 }
65
66 if( Input.GetMouseButtonDown( 1 ) )
67 {
68 pressedPosition = Input.mousePosition;
69 lastGo = RaycastObject( pressedPosition );
70 if( lastGo != null )
71 {
72 lastGo.SendMessage( "OnPressRight", SendMessageOptions.DontRequireReceiver );
73 }
74 }
75 }
File name: InputToEvent.cs
Copy
78 private void Press( Vector2 screenPos )
79 {
80 pressedPosition = screenPos;
81 this.Dragging = true;
82
83 lastGo = RaycastObject( screenPos );
84 if( lastGo != null )
85 {
86 lastGo.SendMessage( "OnPress", SendMessageOptions.DontRequireReceiver );
87 }
88 }
File name: InputToEvent.cs
Copy
90 private void Release( Vector2 screenPos )
91 {
92 if( lastGo != null )
93 {
94 GameObject currentGo = RaycastObject( screenPos );
95 if( currentGo == lastGo ) lastGo.SendMessage( "OnClick", SendMessageOptions.DontRequireReceiver );
96 lastGo.SendMessage( "OnRelease", SendMessageOptions.DontRequireReceiver );
97 lastGo = null;
98 }
99
100 pressedPosition = Vector2.zero;
101 this.Dragging = false;
102 }
SendMessage 158 lượt xem
Gõ tìm kiếm nhanh...