Unity gets the mouse position on the Ugui

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.