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.
锚点参考点是锚点的位置。如果锚点不在一起,四个锚点的位置是根据布置的中心点的位置插值替换的。
unity获取ugui上鼠标位置的更多相关文章
- WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法
一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...
- WPF获取和设置鼠标位置与progressbar的使用方法
一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...
- Unity UGUI在鼠标位置不同时 图片浮动效果
/// <summary> /// 在鼠标位置不同时 图片浮动效果 /// </summary> public class TiltWindow : MonoBehaviour ...
- unity 获取UGUI中的Text字的坐标
using System.Collections; using UnityEngine; using UnityEngine.UI; public class TextMoveHelper : Mon ...
- $(window).scrollTop() 获取当前的鼠标位置 offset.left()指定标签在html中的坐标 offset.top() 指定标签在html中的坐标position() 指定标签相对父(relative)标签的坐标
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里?
原文:WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里? 在 WPF 程序中,我们有 Mouse.GetPosition(IInputElement relativeTo) 方法可以拿到鼠标 ...
- C# 获取鼠标在屏幕上的位置
获取鼠标位置及鼠标单击了哪个按键.private void GetMousePoint() { Point ms = Control.MousePosition; this.label ...
- js获取鼠标位置的各种方法
在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的浏览器下会有不同的结果甚至是有的浏览器下没结果,这篇文章就上鼠标点击位置坐标获取做一些简单的总结,没特殊 ...
- C++获取鼠标位置及全局检测鼠标行为
1.获取鼠标位置(在屏幕的位置) CPoint m_mouse; GetCursorPos(&m_mouse); 2. 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置 ...
随机推荐
- 微信小程序开发——使用第三方插件生成二维码
需求场景: 小程序中指定页面需要根据列表数据生成多张二维码. 实现方案: 鉴于需要生成多张二维码,可以将生成二维码的功能封装到组件中,直接在页面列表循环中调用就好了.也可以给组件添加slot,在页面调 ...
- 将已经存在的项目提交到gitlab的新分支中
将已经存在的项目提交到gitlab中 在gitlab中新增用户jack 登录jack这个git用户,然后创建仓库 mxonline 已经写好了部分功能的项目存放在 D:\>cd D:\pytho ...
- 企业微信域名IP列表
https://res.mail.qq.com/zh_CN/wework_ip/latest.html?st=C98F886B96A94AD2207D9F0B2970B93DFD5A76DF94CED ...
- Spring cloud微服务安全实战-7-3prometheus环境搭建
Prmetheus 主要用来做来Metrics的监控和报警,这张图是官方的架构图. 这是他的核心 它的作用是根据我们的配置去完成数据的采集.服务的发现,以及数据的存储. 这是服务的发现,通过Servi ...
- laravel 解决 Please provide a valid cache path 问题
这是因为laravel的缓存路径没有找到 laravel缓存文件路径是在 config/cache.php中设置,默认存在storage文件夹中 解决: 创建目录: mkdir -p storage/ ...
- EasyNVS摄像机公网全终端无插件网页摄像机直播管理服务运行出现“请求服务不存在或已停止”
背景分析 了解我们产品线的小伙伴都知道,EasyNVR产品主要用于互联网安防直播,通过EasyNVR拉取摄像机的RTSP视频流,客户端通过访问EasyNVR服务端就可以实现音视频流分发.在此情况下会出 ...
- mysql索引数据结构
什么是索引?索引就是排好序的数据结构,可以帮助我们快速的查找到数据 推荐一个网站,可以演示各种数据结构:https://www.cs.usfca.edu/~galles/visualization/A ...
- ou can mix require and export. You can't mix import and module.exports.
ou can mix require and export. You can't mix import and module.exports.
- IDEA下同时使用Git和svn
使用Git时将文件改成Git,Svn时改成svn 修改项目下.idea目录的vcs.xml配置文件. <?xml version="1.0" encoding="U ...
- Tomcat免安装版执行tomcat9.exe闪退
Tomcat免安装版点击startup.bat可成功启动,但是执行tomcat9.exe时却闪退,这是因为免安装版的tomcat不会自动把tomcat注册到windows服务里,需要手动注册. 解决办 ...