OnSerializeTransform
How do I use On Serialize Transform
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: 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 }
File name: PhotonView.cs
Copy
348 internal protected void DeserializeComponent( Component component, PhotonStream stream, PhotonMessageInfo info )
349 {
350 if( component == null )
351 {
352 return;
353 }
354
355 // Use incoming data according to observed type
356 if( component is MonoBehaviour )
357 {
358 ExecuteComponentOnSerialize( component, stream, info );
359 }
360 else if( component is Transform )
361 {
362 Transform trans = (Transform)component;
363
364 switch( onSerializeTransformOption )
365 {
366 case OnSerializeTransform.All:
367 trans.localPosition = (Vector3)stream.ReceiveNext();
368 trans.localRotation = (Quaternion)stream.ReceiveNext();
369 trans.localScale = (Vector3)stream.ReceiveNext();
370 break;
371 case OnSerializeTransform.OnlyPosition:
372 trans.localPosition = (Vector3)stream.ReceiveNext();
373 break;
374 case OnSerializeTransform.OnlyRotation:
375 trans.localRotation = (Quaternion)stream.ReceiveNext();
376 break;
377 case OnSerializeTransform.OnlyScale:
378 trans.localScale = (Vector3)stream.ReceiveNext();
379 break;
380 case OnSerializeTransform.PositionAndRotation:
381 trans.localPosition = (Vector3)stream.ReceiveNext();
382 trans.localRotation = (Quaternion)stream.ReceiveNext();
383 break;
384 }
385 }
386 else if( component is Rigidbody )
387 {
388 Rigidbody rigidB = (Rigidbody)component;
389
390 switch( onSerializeRigidBodyOption )
391 {
392 case OnSerializeRigidBody.All:
393 rigidB.velocity = (Vector3)stream.ReceiveNext();
394 rigidB.angularVelocity = (Vector3)stream.ReceiveNext();
395 break;
396 case OnSerializeRigidBody.OnlyAngularVelocity:
397 rigidB.angularVelocity = (Vector3)stream.ReceiveNext();
398 break;
399 case OnSerializeRigidBody.OnlyVelocity:
400 rigidB.velocity = (Vector3)stream.ReceiveNext();
401 break;
402 }
403 }
404 else if( component is Rigidbody2D )
405 {
406 Rigidbody2D rigidB = (Rigidbody2D)component;
407
408 switch( onSerializeRigidBodyOption )
409 {
410 case OnSerializeRigidBody.All:
411 rigidB.velocity = (Vector2)stream.ReceiveNext();
412 rigidB.angularVelocity = (float)stream.ReceiveNext();
413 break;
414 case OnSerializeRigidBody.OnlyAngularVelocity:
415 rigidB.angularVelocity = (float)stream.ReceiveNext();
416 break;
417 case OnSerializeRigidBody.OnlyVelocity:
418 rigidB.velocity = (Vector2)stream.ReceiveNext();
419 break;
420 }
421 }
422 else
423 {
424 Debug.LogError( "Type of observed is unknown when receiving." );
425 }
426 }
File name: PhotonView.cs
Copy
428 internal protected void SerializeComponent( Component component, PhotonStream stream, PhotonMessageInfo info )
429 {
430 if( component == null )
431 {
432 return;
433 }
434
435 if( component is MonoBehaviour )
436 {
437 ExecuteComponentOnSerialize( component, stream, info );
438 }
439 else if( component is Transform )
440 {
441 Transform trans = (Transform)component;
442
443 switch( onSerializeTransformOption )
444 {
445 case OnSerializeTransform.All:
446 stream.SendNext( trans.localPosition );
447 stream.SendNext( trans.localRotation );
448 stream.SendNext( trans.localScale );
449 break;
450 case OnSerializeTransform.OnlyPosition:
451 stream.SendNext( trans.localPosition );
452 break;
453 case OnSerializeTransform.OnlyRotation:
454 stream.SendNext( trans.localRotation );
455 break;
456 case OnSerializeTransform.OnlyScale:
457 stream.SendNext( trans.localScale );
458 break;
459 case OnSerializeTransform.PositionAndRotation:
460 stream.SendNext( trans.localPosition );
461 stream.SendNext( trans.localRotation );
462 break;
463 }
464 }
465 else if( component is Rigidbody )
466 {
467 Rigidbody rigidB = (Rigidbody)component;
468
469 switch( onSerializeRigidBodyOption )
470 {
471 case OnSerializeRigidBody.All:
472 stream.SendNext( rigidB.velocity );
473 stream.SendNext( rigidB.angularVelocity );
474 break;
475 case OnSerializeRigidBody.OnlyAngularVelocity:
476 stream.SendNext( rigidB.angularVelocity );
477 break;
478 case OnSerializeRigidBody.OnlyVelocity:
479 stream.SendNext( rigidB.velocity );
480 break;
481 }
482 }
483 else if( component is Rigidbody2D )
484 {
485 Rigidbody2D rigidB = (Rigidbody2D)component;
486
487 switch( onSerializeRigidBodyOption )
488 {
489 case OnSerializeRigidBody.All:
490 stream.SendNext( rigidB.velocity );
491 stream.SendNext( rigidB.angularVelocity );
492 break;
493 case OnSerializeRigidBody.OnlyAngularVelocity:
494 stream.SendNext( rigidB.angularVelocity );
495 break;
496 case OnSerializeRigidBody.OnlyVelocity:
497 stream.SendNext( rigidB.velocity );
498 break;
499 }
500 }
501 else
502 {
503 Debug.LogError( "Observed type is not serializable: " + component.GetType() );
504 }
505 }
OnSerializeTransform 118 lượt xem
Gõ tìm kiếm nhanh...