UnityEngine.Networking.NetworkTransform.UnserializeMode2D C# (CSharp) Method

UnserializeMode2D() private method

private UnserializeMode2D ( NetworkReader reader, bool initialState ) : void
reader NetworkReader
initialState bool
return void
        private void UnserializeMode2D(NetworkReader reader, bool initialState)
        {
            if (base.hasAuthority)
            {
                reader.ReadVector2();
                reader.ReadVector2();
                if (this.syncRotationAxis != AxisSyncMode.None)
                {
                    UnserializeRotation2D(reader, this.rotationSyncCompression);
                }
                if (this.syncSpin)
                {
                    UnserializeSpin2D(reader, this.rotationSyncCompression);
                }
            }
            else if (this.m_RigidBody2D != null)
            {
                if (base.isServer && (this.m_ClientMoveCallback2D != null))
                {
                    Vector2 position = reader.ReadVector2();
                    Vector2 velocity = reader.ReadVector2();
                    float rotation = 0f;
                    if (this.syncRotationAxis != AxisSyncMode.None)
                    {
                        rotation = UnserializeRotation2D(reader, this.rotationSyncCompression);
                    }
                    if (!this.m_ClientMoveCallback2D(ref position, ref velocity, ref rotation))
                    {
                        return;
                    }
                    this.m_TargetSyncPosition = (Vector3) position;
                    this.m_TargetSyncVelocity = (Vector3) velocity;
                    if (this.syncRotationAxis != AxisSyncMode.None)
                    {
                        this.m_TargetSyncRotation2D = rotation;
                    }
                }
                else
                {
                    this.m_TargetSyncPosition = (Vector3) reader.ReadVector2();
                    this.m_TargetSyncVelocity = (Vector3) reader.ReadVector2();
                    if (this.syncRotationAxis != AxisSyncMode.None)
                    {
                        this.m_TargetSyncRotation2D = UnserializeRotation2D(reader, this.rotationSyncCompression);
                    }
                }
                if (this.syncSpin)
                {
                    this.m_TargetSyncAngularVelocity2D = UnserializeSpin2D(reader, this.rotationSyncCompression);
                }
                if (base.isServer && !base.isClient)
                {
                    base.transform.position = this.m_TargetSyncPosition;
                    this.m_RigidBody2D.MoveRotation(this.m_TargetSyncRotation2D);
                    this.m_RigidBody2D.velocity = this.m_TargetSyncVelocity;
                }
                else if (this.GetNetworkSendInterval() == 0f)
                {
                    base.transform.position = this.m_TargetSyncPosition;
                    this.m_RigidBody2D.velocity = this.m_TargetSyncVelocity;
                    if (this.syncRotationAxis != AxisSyncMode.None)
                    {
                        this.m_RigidBody2D.MoveRotation(this.m_TargetSyncRotation2D);
                    }
                    if (this.syncSpin)
                    {
                        this.m_RigidBody2D.angularVelocity = this.m_TargetSyncAngularVelocity2D;
                    }
                }
                else
                {
                    Vector2 vector3 = this.m_RigidBody2D.position - this.m_TargetSyncPosition;
                    if (vector3.magnitude > this.snapThreshold)
                    {
                        this.m_RigidBody2D.position = this.m_TargetSyncPosition;
                        this.m_RigidBody2D.velocity = this.m_TargetSyncVelocity;
                    }
                    if ((this.interpolateRotation == 0f) && (this.syncRotationAxis != AxisSyncMode.None))
                    {
                        this.m_RigidBody2D.rotation = this.m_TargetSyncRotation2D;
                        if (this.syncSpin)
                        {
                            this.m_RigidBody2D.angularVelocity = this.m_TargetSyncAngularVelocity2D;
                        }
                    }
                    if (this.m_InterpolateMovement == 0f)
                    {
                        this.m_RigidBody2D.position = this.m_TargetSyncPosition;
                    }
                    if (initialState)
                    {
                        this.m_RigidBody2D.rotation = this.m_TargetSyncRotation2D;
                    }
                }
            }
        }

Usage Example

Esempio n. 1
0
        public static void HandleTransform(NetworkMessage netMsg)
        {
            NetworkInstanceId networkInstanceId = netMsg.reader.ReadNetworkId();
            GameObject        gameObject        = NetworkServer.FindLocalObject(networkInstanceId);

            if (gameObject == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleTransform no gameObject");
                }
                return;
            }
            NetworkTransform component = gameObject.GetComponent <NetworkTransform>();

            if (component == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleTransform null target");
                }
            }
            else if (!component.localPlayerAuthority)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleTransform no localPlayerAuthority");
                }
            }
            else if (netMsg.conn.clientOwnedObjects == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("HandleTransform object not owned by connection");
                }
            }
            else if (netMsg.conn.clientOwnedObjects.Contains(networkInstanceId))
            {
                switch (component.transformSyncMode)
                {
                case TransformSyncMode.SyncNone:
                    return;

                case TransformSyncMode.SyncTransform:
                    component.UnserializeModeTransform(netMsg.reader, initialState: false);
                    break;

                case TransformSyncMode.SyncRigidbody3D:
                    component.UnserializeMode3D(netMsg.reader, initialState: false);
                    break;

                case TransformSyncMode.SyncRigidbody2D:
                    component.UnserializeMode2D(netMsg.reader, initialState: false);
                    break;

                case TransformSyncMode.SyncCharacterController:
                    component.UnserializeModeCharacterController(netMsg.reader, initialState: false);
                    break;
                }
                component.m_LastClientSyncTime = Time.time;
            }
            else if (LogFilter.logWarn)
            {
                Debug.LogWarning("HandleTransform netId:" + networkInstanceId + " is not for a valid player");
            }
        }