public void Init() { if (m_Initialized) { return; } m_Initialized = true; m_SyncTransform = target as NetworkTransform; if (m_SyncTransform.transformSyncMode == NetworkTransform.TransformSyncMode.SyncNone) { if (m_SyncTransform.GetComponent <Rigidbody>() != null) { m_SyncTransform.transformSyncMode = NetworkTransform.TransformSyncMode.SyncRigidbody3D; m_SyncTransform.syncRotationAxis = NetworkTransform.AxisSyncMode.AxisXYZ; EditorUtility.SetDirty(m_SyncTransform); } else if (m_SyncTransform.GetComponent <Rigidbody2D>() != null) { m_SyncTransform.transformSyncMode = NetworkTransform.TransformSyncMode.SyncRigidbody2D; m_SyncTransform.syncRotationAxis = NetworkTransform.AxisSyncMode.AxisZ; EditorUtility.SetDirty(m_SyncTransform); } else if (m_SyncTransform.GetComponent <CharacterController>() != null) { m_SyncTransform.transformSyncMode = NetworkTransform.TransformSyncMode.SyncCharacterController; m_SyncTransform.syncRotationAxis = NetworkTransform.AxisSyncMode.AxisXYZ; EditorUtility.SetDirty(m_SyncTransform); } else { m_SyncTransform.transformSyncMode = NetworkTransform.TransformSyncMode.SyncTransform; m_SyncTransform.syncRotationAxis = NetworkTransform.AxisSyncMode.AxisXYZ; EditorUtility.SetDirty(m_SyncTransform); } } m_TransformSyncMode = serializedObject.FindProperty("m_TransformSyncMode"); m_MovementTheshold = serializedObject.FindProperty("m_MovementTheshold"); m_VelocityThreshold = serializedObject.FindProperty("m_VelocityThreshold"); m_SnapThreshold = serializedObject.FindProperty("m_SnapThreshold"); m_InterpolateRotation = serializedObject.FindProperty("m_InterpolateRotation"); m_InterpolateMovement = serializedObject.FindProperty("m_InterpolateMovement"); m_RotationSyncCompression = serializedObject.FindProperty("m_RotationSyncCompression"); m_SyncSpin = serializedObject.FindProperty("m_SyncSpin"); m_SyncIntervalProperty = serializedObject.FindProperty("syncInterval"); EditorGUI.indentLevel += 1; m_MovementThesholdLabel = new GUIContent("Movement Threshold", "The distance that this object can move without sending a movement synchronization update."); m_VelocityThresholdLabel = new GUIContent("Velocity Threshold", "The minimum velocity difference that will be synchronized over the network."); m_SnapThresholdLabel = new GUIContent("Snap Threshold", "If a movement update puts this object further from its current position that this value, it will snap to the updated position instead of moving smoothly."); m_InterpolateRotationLabel = new GUIContent("Interpolate Rotation Factor", "The larger this number is, the faster the object will interpolate to the target facing direction."); m_InterpolateMovementLabel = new GUIContent("Interpolate Movement Factor", "The larger this number is, the faster the object will interpolate to the target position."); m_RotationSyncCompressionLabel = new GUIContent("Compress Rotation", "How much to compress rotation sync updates.\n\nChoose None for no compression.\n\nChoose Low for a low amount of compression that preserves accuracy.\n\nChoose High for a high amount of compression that sacrifices accuracy."); m_RotationAxisLabel = new GUIContent("Rotation Axis", "Which axis to use for rotation."); m_SyncSpinLabel = new GUIContent("Sync Angular Velocity", "Enable to sync angular velocity."); EditorGUI.indentLevel -= 1; }
void GetNetworkInformation(GameObject gameObject) { m_Transform = gameObject.GetComponent <NetworkTransform>(); m_Rigidbody3D = gameObject.GetComponent <Rigidbody>(); m_Rigidbody2D = gameObject.GetComponent <Rigidbody2D>(); }
public override void OnStartClient() { if (m_VisualizerPrefab != null) { m_NetworkTransform = GetComponent <NetworkTransform>(); CreateLineMaterial(); m_Visualizer = (GameObject)Instantiate(m_VisualizerPrefab, transform.position, Quaternion.identity); } }
void UnserializeModeTransform(NetworkReader reader, bool initialState) { if (hasAuthority) { // this component must read the data that the server wrote, even if it ignores it. // otherwise the NetworkReader stream will still contain that data for the next component. // position reader.ReadVector3(); if (syncRotationAxis != NetworkTransform.AxisSyncMode.None) { NetworkTransform.UnserializeRotation3D(reader, syncRotationAxis, rotationSyncCompression); } return; } if (isServer && m_ClientMoveCallback3D != null) { var pos = reader.ReadVector3(); var vel = Vector3.zero; var rot = Quaternion.identity; if (syncRotationAxis != NetworkTransform.AxisSyncMode.None) { rot = NetworkTransform.UnserializeRotation3D(reader, syncRotationAxis, rotationSyncCompression); } if (m_ClientMoveCallback3D(ref pos, ref vel, ref rot)) { m_TargetSyncPosition = pos; if (syncRotationAxis != NetworkTransform.AxisSyncMode.None) { m_TargetSyncRotation3D = rot; } } else { // rejected by callback return; } } else { // position m_TargetSyncPosition = reader.ReadVector3(); // rotation if (syncRotationAxis != NetworkTransform.AxisSyncMode.None) { m_TargetSyncRotation3D = NetworkTransform.UnserializeRotation3D(reader, syncRotationAxis, rotationSyncCompression); } } }
void SerializeModeTransform(NetworkWriter writer) { // position writer.Write(m_Target.localPosition); // rotation if (m_SyncRotationAxis != NetworkTransform.AxisSyncMode.None) { NetworkTransform.SerializeRotation3D(writer, m_Target.localRotation, syncRotationAxis, rotationSyncCompression); } m_PrevPosition = m_Target.localPosition; m_PrevRotation = m_Target.localRotation; }
void OnValidate() { // root parent of target must have a NetworkTransform if (m_Target != null) { Transform parent = m_Target.parent; if (parent == null) { Debug.LogError("NetworkTransformChild target cannot be the root transform."); m_Target = null; return; } while (parent.parent != null) { parent = parent.parent; } m_Root = parent.gameObject.GetComponent <NetworkTransform>(); if (m_Root == null) { Debug.LogError("NetworkTransformChild root must have NetworkTransform"); m_Target = null; return; } } if (m_Root != null) { // childIndex is the index within all the NetworkChildTransforms on the root m_ChildIndex = UInt32.MaxValue; var childTransforms = m_Root.GetComponents <NetworkTransformChild>(); for (uint i = 0; i < childTransforms.Length; i++) { if (childTransforms[i] == this) { m_ChildIndex = i; break; } } if (m_ChildIndex == UInt32.MaxValue) { Debug.LogError("NetworkTransformChild component must be a child in the same hierarchy"); m_Target = null; } } if (m_SyncRotationAxis < NetworkTransform.AxisSyncMode.None || m_SyncRotationAxis > NetworkTransform.AxisSyncMode.AxisXYZ) { m_SyncRotationAxis = NetworkTransform.AxisSyncMode.None; } if (movementThreshold < 0) { movementThreshold = 0.00f; } if (interpolateRotation < 0) { interpolateRotation = 0.01f; } if (interpolateRotation > 1.0f) { interpolateRotation = 1.0f; } if (interpolateMovement < 0) { interpolateMovement = 0.01f; } if (interpolateMovement > 1.0f) { interpolateMovement = 1.0f; } }