public static bool InClickRect(Vector2 mousePosition, Vector3 worldPosition1, Vector3 worldPosition2, float range) { mousePosition = ConvertMousePointPosition(mousePosition, false); Vector3 targetScreenPosition1 = Camera.current.WorldToScreenPoint(worldPosition1); Vector3 targetScreenPosition2 = Camera.current.WorldToScreenPoint(worldPosition2); // Convert screen position from pixels to points targetScreenPosition1 = EditorHelper.ConvertScreenPixelsToPoints(targetScreenPosition1); targetScreenPosition2 = EditorHelper.ConvertScreenPixelsToPoints(targetScreenPosition2); if (targetScreenPosition1.z < 0) { return(false); } Vector3 closestPoint = MathHelper.ProjectPointOnLineSegment(targetScreenPosition1, targetScreenPosition2, mousePosition); closestPoint.z = 0; if (Vector3.Distance(closestPoint, mousePosition) < range) { return(true); } else { return(false); } }
/// <summary> /// Determines whether a mouse position is within distance of a screen point. Care must be taken with the inputs provided to this method - see the parameter explanations for details. /// </summary> /// <param name="mousePosition">As provided by Event.mousePosition (in points)</param> /// <param name="targetScreenPosition">As provided by Camera.WorldToScreenPoint() (in pixels)</param> /// <param name="screenDistancePoints">Screen distance (in points)</param> /// <returns>True if within the specified distance, False otherwise</returns> public static bool InClickZone(Vector2 mousePosition, Vector2 targetScreenPosition, float screenDistancePoints) { // Convert the mouse position to screen space, but leave in points mousePosition = ConvertMousePointPosition(mousePosition, false); // Convert screen position from pixels to points targetScreenPosition = EditorHelper.ConvertScreenPixelsToPoints(targetScreenPosition); float distance = Vector2.Distance(mousePosition, targetScreenPosition); if (distance <= screenDistancePoints) { return(true); } else { return(false); } }
float CalculateScreenSize(PointTransformer TransformPoint, int pointIndex, Bounds bounds) { float minDistancPoints = float.PositiveInfinity; Vector3 pairPoint = GetPoint(pointIndex); // Process each set component separately, this way we can calculate the min screen size of each active face for (int i = 0; i < 3; i++) { if (pairPoint[i] != 0) { Vector3 sourceDirection = Vector3.zero; sourceDirection[i] = pairPoint[i]; Vector3 extent1Positive = sourceDirection; Vector3 extent1Negative = sourceDirection; Vector3 extent2Positive = sourceDirection; Vector3 extent2Negative = sourceDirection; if (i == 0) // X already set, so set Y and Z { extent1Positive.y = 1; extent1Negative.y = -1; extent2Positive.z = 1; extent2Negative.z = -1; } else if (i == 1) // Y already set, so set X and Z { extent1Positive.x = 1; extent1Negative.x = -1; extent2Positive.z = 1; extent2Negative.z = -1; } else // Z already set, so set X and Y { extent1Positive.x = 1; extent1Negative.x = -1; extent2Positive.y = 1; extent2Negative.y = -1; } Vector3 worldPosition1Positive = TransformPoint(bounds.center + extent1Positive.Multiply(bounds.extents)); Vector3 worldPosition1Negative = TransformPoint(bounds.center + extent1Negative.Multiply(bounds.extents)); Vector3 worldPosition2Positive = TransformPoint(bounds.center + extent2Positive.Multiply(bounds.extents)); Vector3 worldPosition2Negative = TransformPoint(bounds.center + extent2Negative.Multiply(bounds.extents)); //VisualDebug.AddPoints(worldPosition1Positive, worldPosition1Negative, worldPosition2Positive, worldPosition2Negative); Vector3 screenPosition1Positive = Camera.current.WorldToScreenPoint(worldPosition1Positive); Vector3 screenPosition1Negative = Camera.current.WorldToScreenPoint(worldPosition1Negative); Vector3 screenPosition2Positive = Camera.current.WorldToScreenPoint(worldPosition2Positive); Vector3 screenPosition2Negative = Camera.current.WorldToScreenPoint(worldPosition2Negative); float distance1Points = EditorHelper.ConvertScreenPixelsToPoints(Vector2.Distance(screenPosition1Positive, screenPosition1Negative)); float distance2Points = EditorHelper.ConvertScreenPixelsToPoints(Vector2.Distance(screenPosition2Positive, screenPosition2Negative)); minDistancPoints = Mathf.Min(minDistancPoints, distance1Points); minDistancPoints = Mathf.Min(minDistancPoints, distance2Points); } } return(minDistancPoints); }