void OnGUI()
{
if (selectionStarted)
{
Rect rect = GetScreenRect(mousePosition1, Input.mousePosition);
DrawScreenRectBorder(rect, 2, Color.cyan);
rectangleReference = rect;
}
else
{
DrawScreenRectBorder(rectangleReference, 2, Color.cyan);
rectangleReference.position = new Vector2(myCube.position.x, myCube.position.y);
}
}
rectangleReference is Rect and I make a reference to the drawn rect. myCube is a Transform a reference to a 3d cube.
then I tried to change the rectangle position to the cube position but the drawn rectangle is move to the top left position in the screen.
rectangleReference.position = new Vector2(myCube.position.x, myCube.position.y);
The rectangle(rectangleReference) position is vector2 the 3d cube position vector3
This is the method GetScreenREct
Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
{
// Move origin from bottom left to top left
screenPosition1.y = Screen.height - screenPosition1.y;
screenPosition2.y = Screen.height - screenPosition2.y;
// Calculate corners
var topLeft = Vector3.Min(screenPosition1, screenPosition2);
var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
// Create Rect
return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}