public class getmousepos:monobehaviour{public canvas canvas;//canvas private recttransform recttransform;//coordinates void Start () { canvas = gameobject.find ("Canvas"). Getcomponent<canvas> (); Recttransform = Canvas.transform as Recttransform; Can also be written as this. Getcomponent<recttransform> (), but not recommended; } void Update () { if (input.getmousebuttondown (0)) { Vector2 pos; if (Recttransformutility.screenpointtolocalpointinrectangle (Recttransform, Input.mouseposition, Canvas.worldcamera, out POS)) { recttransform.anchoredposition = pos; Debug.Log (POS);}}}
Create a new scene, drag a canvas in the scene, and hang up the script anywhere.
Recttransformutility: Matrix Transform Tool
Recttransformutility.screenpointtolocalpointinrectangle from the screen point to the local point inside the rectangle
Parameters parameters
Rect |
The Recttransform to find a point inside. |
Cam |
The camera associated with the screen space position. |
Screenpoint |
Screen space position. |
Localpoint |
Point in local space of the rect transform. |
Returns
BOOL Returns true if the plane of the recttransform is hits, regardless of whether the point is inside the rectangle.
Description description
Transform A-screen space point-a position in the local space of a recttransform that's on the plane of its rectangle.
The screen space point is converted to a local position inside the rectangle transformation, which is on its rectangular plane.
The cam parameter should is the camera associated with the. For a recttransform in a Canvas set to screen Space-overlay mode, the cam parameter should is null.
The cam parameter should be the screen point that the camera is associated with. For rectangle transformations on the canvas set this screen space to-overlay mode, the cam parameter should be empty.
When screenpointtolocalpointinrectangle are used from within an event handler that provides a Pointereventdata object, the Correct camera can be obtained by using pointereventdata.entereventdata (for hover functionality) or pointereventdata.pres Seventcamera (for click functionality). This would automatically use the correct camera (or null) for the given event.
When Screenpointtolocalpointinrectangle provides a Pointereventdata object from within the event handler, the camera can be used by using the Pointereventdata.entereventdata ( for hover function) or pointereventdata.presseventcamera (for click function) is obtained. The function will automatically use the correct camera (or null) for the specified event.
Recttransform Rectangle Transform
Recttransform.anchoredposition Anchor Point Location
The position of the pivot of this recttransform relative to the anchor reference point.
The center point position of the rectangle transformation relative to the anchor point reference point.
The anchor reference point was where the anchors is. If the anchors is not together, the four anchor positions is interpolated according to the pivot placement.
The anchor reference point is the position of the anchor point. If the anchor points are not together, the position of the four anchor points is replaced by the position interpolation of the center point of the layout.
Unity gets the mouse position on the Ugui