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本地坐标的转换的更多相关文章

  1. unity---世界坐标和本地坐标的转换

    会受到缩放的影响 用的比较多的是 本地坐标转换为世界坐标,比如在自己周围释放物体 当物体发生旋转,方向会发生变化

  2. unity,set ugui rectTransform anchor by script

    如果想用代码实现与下面面板相同的功能 试验可知改变上面选项下面四个值也随之变化: 所以说明二者是一回事儿. 因此,只要通过代码修改RectTransform的anchorMax和anchorMin成员 ...

  3. UGUI世界坐标转换为UI本地坐标(游戏Hud的实现)

    实现世界坐标的原理是: 世界坐标和UGUI的坐标分属两个坐标系,他们之间是无法进行转换的,需要通过屏幕坐标系来进行转换(因为屏幕坐标是固定的),即先将游戏场景中的世界坐标通过游戏场景Camera转化为 ...

  4. Unity UGUI RectTransform图解

    UGUI RectTransform.Unity RectTransform详解 The first:look look API. http://docs.unity3d.com/ScriptRefe ...

  5. Unity 本地坐标到世界坐标,世界坐标到本地坐标

    世界=>本地 public GameObject mTarget; public GameObject mPar; //这个注意一定要是mTarget的第一父物体. // Use this fo ...

  6. 在Unity中使用UGUI修改Mesh绘制几何图形

    在商店看到这样一个例子,表示很有兴趣,他们说是用UGUI做的.我想,像这种可以随便变形的图形,我第一个就想到了网格变形. 做法1: 细心的朋友应该会发现,每个UGUI可见元素,都有一个‘Canvas ...

  7. 关于Unity中的UGUI优化,你可能遇到这些问题

    https://blog.uwa4d.com/archives/QA_UGUI-1.html 关于Unity中的UGUI优化,你可能遇到这些问题 作者:admin / 时间:2016年11月08日 / ...

  8. Flex各类型坐标转换(全局、本地、内容坐标间转换)

    Flex包含3种坐标:全局坐标.本地坐标.内容坐标 全局坐标:stage级别,坐标原点为舞台的左上角,如MouseEvent的stageX.stageY坐标. 本地坐标:组件级别的坐标系,相对坐标,坐 ...

  9. Cesium之球心坐标与本地坐标

    1球心坐标(ECEF)与本地坐标(NEU) 假如你来到一个陌生城市,你很可能需要问路.通常会告诉你向北走100米,右转,向东走100米,理解起来很直观.你给儿子买了一个地球仪,你从北京(39,115) ...

随机推荐

  1. 启动maven项目时报错Failed to start component [StandardEngine[Tomcat]]: A child container failed during start

    详细错误信息:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on ...

  2. tp5 回滚事务记录,其中一条语句报错,全部回滚

    #################################### 测试事务 // 启动事务 Db::startTrans(); try { //插入行为表 $data = [ 'userId' ...

  3. linux上安装phpstudy

    摘要:安装:wget-chttp://lamp.phpstudy.net/phpstudy.bin chmod+xphpstudy.bin  #权限设置./phpstudy.bin#运行安装用时十到几 ...

  4. [hibernate]log4jdbc日志输出完整SQL语句

    1.在maven引入: <dependency> <groupId>log4j</groupId> <artifactId>log4j</arti ...

  5. powershell命令教程

    启动 powershell #字符串操作 对象操作 "hello".Length #进程操作 PS C:\> notepad PS C:\> $process=get- ...

  6. 每个Xcode开发者应该知道的几个使用技巧

    1.快速打开 快速打开(Open Quickly)命令在Xcode的File菜单中,当然,用快捷键Command+Shift+O会更方便一些.这个命令可以开启一个小窗格用来快速搜索浏览文件.类.算法以 ...

  7. LODOP直接导出图片不弹框

    之前有博文测试了导出图片的图片长度关系,是直接弹窗的选择保存路径的方式:Lodop导出图片,导出单页内容的图片最近测试下不弹窗保存图片是否可以,样例是保存的excel,测试了下图片,图片也是可以的,该 ...

  8. Ubuntu 18.04 上使用xrdp远程桌面连接

    参考:https://blog.csdn.net/qq_25556149/article/details/82216190 1,环境查看 2,安装 xrdp.tightvncserver apt-ge ...

  9. c# 子窗体居中父窗体

    1.设置CenterParent不管用.只好用代码控制. frmRunning_ = new FrmRunning(); frmRunning_.StartPosition = FormStartPo ...

  10. 利用matlab自带函数graycoprops 实现基于共生矩阵的遥感图像纹理特征分析

    close all;clear all;clc;I = imread('yaogan2.jpg');HSV = rgb2hsv(I);Hgray = rgb2gray(HSV);% 计算64位灰度共生 ...