Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     internal protected void DeserializeComponent( Component component, PhotonStream stream, PhotonMessageInfo info )
     {
         if( component == null )
         {
             return;
         }

         // Use incoming data according to observed type
         if( component is MonoBehaviour )
         {
             ExecuteComponentOnSerialize( component, stream, info );
         }
         else if( component is Transform )
         {
             Transform trans = (Transform)component;

             switch( onSerializeTransformOption )
             {
             case OnSerializeTransform.All:
                 trans.localPosition = (Vector3)stream.ReceiveNext();
                 trans.localRotation = (Quaternion)stream.ReceiveNext();
                 trans.localScale = (Vector3)stream.ReceiveNext();
                 break;
             case OnSerializeTransform.OnlyPosition:
                 trans.localPosition = (Vector3)stream.ReceiveNext();
                 break;
             case OnSerializeTransform.OnlyRotation:
                 trans.localRotation = (Quaternion)stream.ReceiveNext();
                 break;
             case OnSerializeTransform.OnlyScale:
                 trans.localScale = (Vector3)stream.ReceiveNext();
                 break;
             case OnSerializeTransform.PositionAndRotation:
                 trans.localPosition = (Vector3)stream.ReceiveNext();
                 trans.localRotation = (Quaternion)stream.ReceiveNext();
                 break;
             }
         }
         else if( component is Rigidbody )
         {
             Rigidbody rigidB = (Rigidbody)component;

             switch( onSerializeRigidBodyOption )
             {
             case OnSerializeRigidBody.All:
                 rigidB.velocity = (Vector3)stream.ReceiveNext();
                 rigidB.angularVelocity = (Vector3)stream.ReceiveNext();
                 break;
             case OnSerializeRigidBody.OnlyAngularVelocity:
                 rigidB.angularVelocity = (Vector3)stream.ReceiveNext();
                 break;
             case OnSerializeRigidBody.OnlyVelocity:
                 rigidB.velocity = (Vector3)stream.ReceiveNext();
                 break;
             }
         }
         else if( component is Rigidbody2D )
         {
             Rigidbody2D rigidB = (Rigidbody2D)component;

             switch( onSerializeRigidBodyOption )
             {
             case OnSerializeRigidBody.All:
                 rigidB.velocity = (Vector2)stream.ReceiveNext();
                 rigidB.angularVelocity = (float)stream.ReceiveNext();
                 break;
             case OnSerializeRigidBody.OnlyAngularVelocity:
                 rigidB.angularVelocity = (float)stream.ReceiveNext();
                 break;
             case OnSerializeRigidBody.OnlyVelocity:
                 rigidB.velocity = (Vector2)stream.ReceiveNext();
                 break;
             }
         }
         else
         {
             Debug.LogError( "Type of observed is unknown when receiving." );
         }
     }