unity获取ugui上鼠标位置

public class GetMousePos : MonoBehaviour
{
    public Canvas canvas;//画布
    private RectTransform rectTransform;//坐标

    void Start()
    {
        canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
        rectTransform = canvas.transform as RectTransform; //也可以写成this.GetComponent<RectTransform>(),但是不建议;

    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector2 pos;
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, canvas.worldCamera, out pos))
            {
                rectTransform.anchoredPosition = pos;
                Debug.Log(pos);
            }
        }
    }
}

  新建场景,在场景中拖一个画布(Canvas),然后随便找个地方挂上这个脚本就好了。

  RectTransformUtility:矩阵变换工具

  RectTransformUtility.ScreenPointToLocalPointInRectangle 从屏幕点到矩形内的本地点

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 hit, regardless of whether the point is inside the rectangle.

Description 描述

Transform a screen space point to a position in the local space of a RectTransform that is on the plane of its rectangle.

屏幕空间点转换为矩形变换内部的本地位置,该点在它的矩形平面上。

The cam parameter should be the camera associated with the screen point. For a RectTransform in a Canvas set to Screen Space - Overlay mode, the cam parameter should be null.

该cam 参数应该是该相机关联的屏幕点。对于在画布上的矩形变换设置该屏幕空间为-Overlay模式,cam 参数应该为空。

When ScreenPointToLocalPointInRectangle is 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.pressEventCamera (for click functionality). This will automatically use the correct camera (or null) for the given event.

当ScreenPointToLocalPointInRectangle从事件处理器内部提供一个PointerEventData对象被使用时,相机可以通过使用PointerEventData.enterEventData(为悬停功能)或者 PointerEventData.pressEventCamera(为单击功能)被获取。该函数将会自动对指定事件使用正确的相机(或者空)。

RectTransform矩形变换

RectTransform.anchoredPosition 锚点位置

The position of the pivot of this RectTransform relative to the anchor reference point.

该矩形变换相对于锚点参考点的中心点位置。

The anchor reference point is where the anchors are. If the anchors are not together, the four anchor positions are interpolated according to the pivot placement.

锚点参考点是锚点的位置。如果锚点不在一起,四个锚点的位置是根据布置的中心点的位置插值替换的。

时间: 2024-08-24 15:09:09

unity获取ugui上鼠标位置的相关文章

Unity UGUI在鼠标位置不同时 图片浮动效果

1 /// <summary> 2 /// 在鼠标位置不同时 图片浮动效果 3 /// </summary> 4 public class TiltWindow : MonoBehaviour 5 { 6 public Vector2 range = new Vector2(5f, 3f); 7 8 Transform mTrans; 9 Quaternion mStart; 10 Vector2 mRot = Vector2.zero; 11 12 void Start () 1

WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里?

原文:WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里? 在 WPF 程序中,我们有 Mouse.GetPosition(IInputElement relativeTo) 方法可以拿到鼠标当前相对于某个 WPF 控件的位置,也可以通过在 MouseMove 事件中通过 e.GetPosition(IInputElement relativeTo) 方法拿到同样的信息.不过,在任意时刻去获取鼠标位置的时候,如果鼠标在窗口之外,将获取到什么点呢? 本文将介绍鼠标在窗口之外时获取到的鼠标位

C++获取鼠标位置及全局检测鼠标行为

1.获取鼠标位置(在屏幕的位置)  CPoint m_mouse; GetCursorPos(&m_mouse); 2. 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置 ClientToScreen(this->m_hwnd,m_mouse);  //客户端位置转化为屏幕位置 ScreenToClient(this->m_hwnd,m_mouse) ;  //屏幕转化为客户端 3.获取控件关于在屏幕的位置 CRect  rc GetWindowRect(&

js获取浏览器宽高、网页宽高、屏幕宽高、鼠标位置等(带图片说明)

网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;(点击查看大图) 网页可见区域宽: document.body.offsetWidth (包括边线的宽);网页可见区域高: document.body.offsetHeight (包括边线的宽);(点击查看大图)有没有发现,offsetWidth和clientWidth的区别,offsetWidt是连滚动条一起包含在内的. 网页正文全文宽: documen

DevExpress 获取gridview 鼠标位置所在单元格

偶尔会有一些需要获取 gridview 鼠标位置所在单元格,从而实现某些特定功能的需求,经过一番研究,得出以下实现方法: GridHitInfo _info; Point _pt = gridview1.GridControl.PointToClient(Control.MousePosition); _info = gridview1.CalcHitInfo(_pt); bool IsInColumn =_info.InColumn;//判断是否在表头 GridColumn col= _inf

js获取鼠标位置的各种方法

在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的浏览器下会有不同的结果甚至是有的浏览器下没结果,这篇文章就上鼠标点击位置坐标获取做一些简单的总结,没特殊声明代码在IE8,FireFox,Chrome下进行测试兼容 鼠标点击位置坐标 相对于屏幕 如果是涉及到鼠标点击确定位置相对比较简单,获取到鼠标点击事件后,事件screenX,screenY获取的是点击位置相对于屏幕的左边距与上边距,不考虑iframe因素,不同浏览器下表现的还算一致. functi

js获取鼠标位置

1.PageX/PageX:鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化2.clientX/clientY:鼠标在页面上可视区域的位置,从浏览器可视区域左上角开始,即是以浏览器滑动条此刻的滑动到的位置为参考点,随滑动条移动 而变化. 可是悲剧的是,PageX只有FF特有,IE这个悲剧没有啊T_T,所以大牛们想出了一个办法PageY=clientY+scrollTop-clientTop;(只讨论Y轴,X轴同理,下同)3.screenX/screenY:鼠标在屏幕

如何获取鼠标位置

获取鼠标位置,首先需要加载js文件: 然后设置一个div,给定大小: 最后进行具体操作: //首先要先设置一个div,给定大小 <div id="m"></div> //进行操作 <script type="text/javascript"> var x,y,T,L; T=$("#m").offset().top;//div离开左端的距离 L=$("#m").offset().left;//

【Unity笔记】物体朝着鼠标位置移动

其实思路也很简单,就是先获取到鼠标的坐标,然后赋值给目标物体即可. void Update(){ // 物体跟随鼠标移动 Vector2 mousePos = Input.mousePosition; gameObject.transform.position = mousePos; } 这是物体即时改变位置的做法,如果想要物体朝鼠标位置缓慢移动(有过程时间),可以尝试用Vector2.Lerp().