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.

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

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

  1. WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法

    一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...

  2. WPF获取和设置鼠标位置与progressbar的使用方法

    一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...

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

    /// <summary> /// 在鼠标位置不同时 图片浮动效果 /// </summary> public class TiltWindow : MonoBehaviour ...

  4. unity 获取UGUI中的Text字的坐标

    using System.Collections; using UnityEngine; using UnityEngine.UI; public class TextMoveHelper : Mon ...

  5. $(window).scrollTop() 获取当前的鼠标位置 offset.left()指定标签在html中的坐标 offset.top() 指定标签在html中的坐标position() 指定标签相对父(relative)标签的坐标

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

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

    原文:WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里? 在 WPF 程序中,我们有 Mouse.GetPosition(IInputElement relativeTo) 方法可以拿到鼠标 ...

  7. C# 获取鼠标在屏幕上的位置

    获取鼠标位置及鼠标单击了哪个按键.private void GetMousePoint() {     Point ms = Control.MousePosition;     this.label ...

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

    在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的浏览器下会有不同的结果甚至是有的浏览器下没结果,这篇文章就上鼠标点击位置坐标获取做一些简单的总结,没特殊 ...

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

    1.获取鼠标位置(在屏幕的位置)  CPoint m_mouse; GetCursorPos(&m_mouse); 2. 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置 ...

随机推荐

  1. SQLServer charindex函数, 查 某个字符 或 某个字符串 在 另一个字符串中的位置

    一:charindex()语法 CHARINDEX ( expression1 , expression2 [ , start_location ] ) 解析: expression1 必需 ---要 ...

  2. 错误详情:已取消该操作。--------- 在 System.Threading.CancellationToken.ThrowOperationCanceledException()

    错误详情:已取消该操作.--------- 在 System.Threading.CancellationToken.ThrowOperationCanceledException() 在 Syste ...

  3. EasyDSS高性能RTMP/FLV/HLS(m3u8)/RTSP流媒体服务器技术的HTTP QueryString URL的C++实现方案

    EasyDSS支持HTTP GET接口访问,我们需要获取url的各种参数信息 比如 http://ip:port/action?a=1&b=2&c=3 我们需要知道对应的a.b.c的值 ...

  4. OpenGL基础汇总

    OpenGL(应用程序接口 即API)——用于访问图形硬件的可编程特性 1. 各种矩阵: 世界矩阵(World Matrix):世界矩阵确定一个统一的世界坐标,用于组织独立的物体形成一个完整的场景; ...

  5. JS字符串数字前面加加号会变成数字类型

    JS中一个字符串中只有数字,如果该字符串前面加了个加号,这个数值就变成了number类型.如本文测试中,用lodop打印二维码,最后一个参数是一个字符串,在前面加了加号和不在前面加加号,通过控制台输出 ...

  6. 【SSH进阶之路】Hibernate映射——多对多关联映射(八)

    上篇博文[SSH进阶之路]Hibernate映射——一对多关联映射(七),我们介绍了一对多关联映射,它是多对多关联映射的基础. 多对多映射是现实生活中最常见的映射,也是最容易理解的映射.废话少说,直接 ...

  7. .Net Core 定时任务TimeJob

    转载自:https://blog.csdn.net/u013711462/article/details/53449799 定时任务 Pomelo.AspNetCore.TimedJob Pomelo ...

  8. Spring Boot拦截器实现并和swagger集成后使用拦截器的配置问题

    1. 定义拦截器 LoginInterceptor LoginInterceptor.java是整个登录认证模块中的核心类之一,它实现了HandlerInterceptor类,由它来拦截并过滤到来的每 ...

  9. (CSDN迁移) 替换字符串中的空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. public ...

  10. Django文档阅读之查询

    创建对象 为了在Python对象中表示数据库表数据,Django使用直观的系统:模型类表示数据库表,该类的实例表示数据库表中的特定记录. 要创建对象,请使用模型类的关键字参数对其进行实例化,然后调用s ...