Untitled
unknown
csharp
7 months ago
907 B
9
Indexable
/// <summary> Method that updates the tooltip's position to follow the mouse cursor, clamping within screen bounds. </summary>
private void UpdateTooltipPosition()
{
Vector3 mousePosition = Input.mousePosition + _positionOffset * _parentCanvas.scaleFactor;
// Get the tooltip size in screen space, including canvas scaling.
Vector2 tooltipSize = _rectTransform.rect.size * _parentCanvas.scaleFactor;
// Clamp the X and Y positions so that the tooltip doesn't overflow the screen.
float clampedX = Mathf.Clamp(mousePosition.x, 0, Screen.width - tooltipSize.x);
float clampedY = Mathf.Clamp(mousePosition.y, 0, Screen.height - tooltipSize.y);
// Convert the clamped position from screen space to world space and set it to the rect position.
_rectTransform.position = CameraManager.Instance.mainCamera.ScreenToWorldPoint(new Vector3(clampedX, clampedY, 100));
}Editor is loading...
Leave a Comment