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.

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

转载于:https://www.cnblogs.com/lanrenqilanming/p/6610712.html

unity获取ugui上鼠标位置相关推荐

  1. UG\NX二次开发 获取曲线上某个位置的点坐标、切线矢量、主法线矢量、副法线矢量 UF_MODL_ask_curve_props

    文章作者:里海 来源网站:https://blog.csdn.net/WangPaiFeiXingYuan 简介: UG\NX二次开发 获取曲线上某个位置的点坐标.切线矢量.主法线矢量.副法线矢量 U ...

  2. Unity在UGUI上使用Polygon Collider 2D实现不规则图案匹配

    在一个项目中需要对衣服部位进行颜色填充,由于衣服每个部位都是不规则的图形,所以我采用Polygon Collider2D组件作为碰撞器进行射线检测. 如果使用2D sprite对象赋予好图片之后再添加 ...

  3. html获取附件上传位置,html中上传文件得到文件的绝对路径!

    html中上传文件得到文件的绝对路径! HTML中使用input type="file"上传文件时,代码中只能得到文件的名称,而有些特殊的需要要求我们必须得到上传文件的绝对路径,为 ...

  4. Unity2D 实现UGUI滚动鼠标滑轮以鼠标位置点为中心缩放图片

    先放参考文章: Unity3d UGUI以鼠标位置点为中心缩放图片(含项目源码)https://blog.csdn.net/qq_33789001/article/details/117749837 ...

  5. 【Python_PyQtGraph 学习笔记(三)】基于GraphicsLayoutWidget实现显示曲线对象 鼠标位置处坐标的功能

    基于GraphicsLayoutWidget实现显示曲线对象 鼠标位置处坐标的功能 前言 在PyQtGraph的官方例程中有此功能的实现,可参考Crosshair / Mouse interactio ...

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

    在 WPF 程序中,我们有 Mouse.GetPosition(IInputElement relativeTo) 方法可以拿到鼠标当前相对于某个 WPF 控件的位置,也可以通过在 MouseMove ...

  7. Unity 在zSpace上使用鼠标控制相机旋转和鼠标指引式放大缩小,在触屏上手势位置为中心放大缩小

    在zSpace上使用鼠标控制相机旋转和鼠标指引式放大缩小,在触屏上手势位置为中心放大缩小 鼠标和触屏的操作 下面展示一些 内联代码片. using System; using System.Colle ...

  8. 用 javascript 获取当页面上鼠标(光标)位置

    用 javascript 获取当页面上鼠标(光标)位置在许多情况下都会用到,比如拖放,悬停提示(tooltip) 等等.当然,这里我们依然要面对浏览器的兼容问题,在不同的浏览器下,对这些相关的属性处理 ...

  9. 【Unity】Input——检测鼠标、键盘、手柄输入、鼠标在屏幕上的位置等等

    1.鼠标在屏幕上的位置 using System.Collections; using System.Collections.Generic; using UnityEngine;public cla ...

最新文章

  1. “自拍神器”贴心实用功能大曝光
  2. 动捕技术是拯救VR体验的关键,但如何落地却已成为世界难题
  3. luvit 被忽视的lua 高性能框架(仿nodejs)
  4. Linux 下的NFS server 架设基础及方法
  5. php pdo 执行多条语句,php – 在pdo预处理语句中执行多插入或多个不同插入是更好/更快?...
  6. 【论文解读】突破置换模块计算瓶颈,MSRA开源轻量版HRNet,超越主流轻量化网络!|CVPR2021...
  7. 下拉导航中绝对定位与相对定位问题
  8. Mac 快速修改 hosts 文件
  9. 李彦宏:百度智能汽车预计2023年和大家见面
  10. noip2017day2
  11. 拓端tecdat|如何利用深度学习诊断心脏病
  12. gpt-2 文章自动生成_有助于您理解GPT-3的文章
  13. 百度地图聚合找房开发
  14. 2018 蓝桥杯省赛 B 组模拟赛(一)青出于蓝胜于蓝
  15. 现阶段有哪些方式可以快速感知元宇宙?
  16. bad_pool_caller蓝屏故障分析
  17. php过滤微信表情符号的正则表达式方法
  18. js重新加载页面的方法
  19. 成为新时代大数据工程师要满足哪些要求?
  20. gg修改器偏移量修改_烧饼修改器地址偏移计算器功能使用说明

热门文章

  1. c#反混淆工具de4dot 一般混淆都可以解决
  2. 简单入门Javascript正则表达式
  3. 小扎不哭!FB又陷数据泄露风波,9000万用户受影响
  4. 第一阶段:前端开发_Mysql——表与表之间的关系
  5. vim编辑器之按键说明
  6. 嵌入式开发-lesson9-顺序结构程序设计
  7. OpenSSH 密钥管理:RSA/DSA 认证(转载)
  8. EasyUI,二级页面内容的操作
  9. 常用命令-tar 加密
  10. 在Eclipse中查看JDK类库的源代码