Unity determines whether the mouse is in the source code of the ui's Recttransform

Record the scheme of judging whether the mouse click is within a certain window.

When meeting a requirement, the interface has two layers, the model of the back layer is illuminated by 3Dcamera, and the window of the front layer is displayed by 2DCamera, but when the window area blocks the 3D area, the 3D area can still respond to mouse operations (can be in the overlapping area drag the back model). I hope that when there is a 2D window in front of the 3D area, let the mouse click and drag only respond to the 2D window.

The solution is to judge that when the mouse is dragged in the 2D window, the mouse operations such as dragging and rotating the 3D model are invalid.

The RectTransform.RectangleContainsScreenPoint method, as the name suggests, determines whether the matrix range contains screen points. The parameters are: position matrix, screen points, and Camera used.

 
 
  1. if(RectTransformUtility.RectangleContainsScreenPoint(transform.GetComponent<RectTransform>(),

  2. Input.mousePosition, Cameras.UI))

  3. {

  4. Debug.Log("在遮挡范围内");

  5. }

 The method is very simple, the main thing is to know this API, the following is the translation of other APIs of RectTransformUtility.

Official description: Unity - Scripting API: RectTransformUtility

 7 methods are provided, listed separately

① void FlipLayoutAxes(RectTransform rect, bool keepPositioning, bool recursive);

Summary: Flips the horizontal and vertical axes of a RectTransform, including flipping of size and alignment.

parameter:

rect - the RectTransform to flip;

keepPositioning - whether to keep its own position, if true, flip around the axis, if false, flip inside the parent RectTransform;

recursive - whether child elements are flipped together.

② void FlipLayoutOnAxis(RectTransform rect, int axis, bool keepPositioning, bool recursive);

Summary: Flips the alignment of a RectTransform along the horizontal or vertical direction. For example, after the control from left to right is flipped horizontally, from right to left

parameter:

axis - the axis to flip along. 0 is horizontal, 1 is vertical

③ Vector2 PixelAdjustPoint(Vector2 point, Transform elementTransform, Canvas canvas);

Summary: Convert a given point on the screen to a pixel-correct point.

Returns the result: the pixel-transformed point.

④ Rect PixelAdjustRect(RectTransform rectTransform, Canvas canvas);

Summary: Given a RectTransform, returns the corners in pixel-accurate coordinates.

Returns the result: the pixel-adjusted matrix transformation.

⑤ bool RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint, Camera cam);

Summary: Whether the RectTransform contains the screen point as seen from the given camera.

Return Result: Returns True if the point is within the matrix range, otherwise returns False.

⑥ bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint);

Summary: Convert a screen space point to a RectTransfrom local space position.

parameter:

rect - the RectTransfom in which to find a point.

cam - the Camera associated with the screen space point.

screenPoint - the screen space point

localPoint - the point in the local space

⑦ bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint);

Summary: Convert a screen space point to a RectTransform position in world space.

Copyright statement: This article is an original article by sinat_34337520 and follows the  CC 4.0 BY-SA  copyright agreement. Please attach the original source link and this statement for reprinting.

Link to this article: Unity determines whether the mouse click is within a RectTransform of a window - Programmer Sought

 

Guess you like

Origin blog.csdn.net/avi9111/article/details/122541177