Unity 屏幕坐标到UGUI RectTransform本地坐标的转换
public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint);
rect: 对应的 RectTransform 的引用
screenPoint: 位置,基于屏幕坐标系
cam: 相机的引用,如果Canvas的Render Mode 为 Screen Space - Camera 模式,则需要填入 Render Camera 对应的引用
localPoint: rect 本地坐标系下的坐标(原点(0,0)位置受Anchor的影响)
参考:
https://docs.unity3d.com/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems; public class GlobalTest : MonoBehaviour
{
public Canvas canvas;
Text uiText;
RectTransform canvasRect;
RectTransform textRect;
void Start()
{
uiText = canvas.GetComponentInChildren<Text>();
canvasRect = canvas.GetComponent<RectTransform>();
textRect = uiText.GetComponent<RectTransform>(); Debug.Log(textRect.anchoredPosition); } void Update()
{
if (Input.GetMouseButtonUp())
{
Vector2 outVec;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect,Input.mousePosition,null,out outVec))
{
Debug.Log("Setting anchored positiont to: " + outVec);
textRect.anchoredPosition = outVec;
}
} }
}
参考:
https://forum.unity.com/threads/issues-with-recttransformutility-screenpointtolocalpointinrectangle.437246/
再说一句
也可以从UGUI的EventSystem中获得鼠标基于屏幕坐标系的点击位置
public void OnPointerDown(PointerEventData pointerEventData)
{
//Output the name of the GameObject that is being clicked
Debug.Log(name + "Game Object Click in Progress");
}
参考:
https://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.html
Unity 屏幕坐标到UGUI RectTransform本地坐标的转换的更多相关文章
- unity---世界坐标和本地坐标的转换
会受到缩放的影响 用的比较多的是 本地坐标转换为世界坐标,比如在自己周围释放物体 当物体发生旋转,方向会发生变化
- unity,set ugui rectTransform anchor by script
如果想用代码实现与下面面板相同的功能 试验可知改变上面选项下面四个值也随之变化: 所以说明二者是一回事儿. 因此,只要通过代码修改RectTransform的anchorMax和anchorMin成员 ...
- UGUI世界坐标转换为UI本地坐标(游戏Hud的实现)
实现世界坐标的原理是: 世界坐标和UGUI的坐标分属两个坐标系,他们之间是无法进行转换的,需要通过屏幕坐标系来进行转换(因为屏幕坐标是固定的),即先将游戏场景中的世界坐标通过游戏场景Camera转化为 ...
- Unity UGUI RectTransform图解
UGUI RectTransform.Unity RectTransform详解 The first:look look API. http://docs.unity3d.com/ScriptRefe ...
- Unity 本地坐标到世界坐标,世界坐标到本地坐标
世界=>本地 public GameObject mTarget; public GameObject mPar; //这个注意一定要是mTarget的第一父物体. // Use this fo ...
- 在Unity中使用UGUI修改Mesh绘制几何图形
在商店看到这样一个例子,表示很有兴趣,他们说是用UGUI做的.我想,像这种可以随便变形的图形,我第一个就想到了网格变形. 做法1: 细心的朋友应该会发现,每个UGUI可见元素,都有一个‘Canvas ...
- 关于Unity中的UGUI优化,你可能遇到这些问题
https://blog.uwa4d.com/archives/QA_UGUI-1.html 关于Unity中的UGUI优化,你可能遇到这些问题 作者:admin / 时间:2016年11月08日 / ...
- Flex各类型坐标转换(全局、本地、内容坐标间转换)
Flex包含3种坐标:全局坐标.本地坐标.内容坐标 全局坐标:stage级别,坐标原点为舞台的左上角,如MouseEvent的stageX.stageY坐标. 本地坐标:组件级别的坐标系,相对坐标,坐 ...
- Cesium之球心坐标与本地坐标
1球心坐标(ECEF)与本地坐标(NEU) 假如你来到一个陌生城市,你很可能需要问路.通常会告诉你向北走100米,右转,向东走100米,理解起来很直观.你给儿子买了一个地球仪,你从北京(39,115) ...
随机推荐
- Flutter移动电商实战 --(14)首页_拨打电话操作
拨打电话的功能在app里也很常见,比如一般的外卖app都会有这个才做.其实Flutter本身是没给我们提供拨打电话的能力的,那我们如何来拨打电话那? 1.编写店长电话模块 这个小伙伴们一定轻车熟路了, ...
- django 后台静态文件不显示
原文链接 https://my.oschina.net/VASKS/blog/874270 django admin svg 不显示.后台显示 xx.svg 200 但浏览器就是不显示. 百度了一圈, ...
- 源码编译vi过程中进行配置时报“checking if compile and link flags for Python are sane... no: PYTHON DISABLED”怎么办?
答: 需要安装python开发库(如果不安装这个库,那么在配置时执行./configure --enable-pythoninterp=yes将不会生效,以至于vi的python特性并没有被开启) 如 ...
- MapReduce编程实例
MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...
- [Python]Excel编程示例教程(openpyxl)
1 前言(Preface) 博文背景:论文实验中有一大堆数据集需要观测其数据特征,通过人体肉眼,难以直观感受,故而准备通过生成Excel,可视化其评测数据. We know: Excel便于可视化分析 ...
- html table设置成强制不换行
在html文件中添加如下代码: <style type="text/css"> table td{word-break: keep-all;white-space:no ...
- 初始Hibernate框架
首先我们需要先创建一个案例 构建一个Student 的实体类 private String name; private Integer age; private Integer id; 在SRC根目录 ...
- laravel5.5的定时任务详解(demo)
原文地址:https://blog.csdn.net/LJFPHP/article/details/80417552
- 码云配置WebHook自动更新
配置项目提交到git的时候自动同步服务器代码 一.在服务器项目跟目录新建文件hook.php 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php $json = ...
- github pages + hexo 搭建 blog 遇到的问题
一. ERROR Deployer not found: git $ hexo d ERROR Deployer not found: git npm install --save hexo-depl ...