obsidian/笔记文件/2.笔记/Unity UGUI获取鼠标在屏幕的准确点击位置.md
2025-03-26 00:02:56 +08:00

6.2 KiB
Raw Blame History

#unity/日常积累

一、方式一 1.屏幕坐标 屏幕坐标的起点位置 左下角为00右上角为Screen.widthScreen.height。我们在Unity中使用Input.mousePosition获取的鼠标的坐标就是屏幕坐标。将如下脚本挂在canvas上测试结果如下

public class UIClick : MonoBehaviour
{
    public Transform img; //要改变位置的UI

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            Vector2 pos = Input.mousePosition;
            Debug.Log("screen w:" + Screen.width + ", h:" + Screen.height);
            Debug.Log("click pos x:" + pos.x + ",pos y:" + pos.y);
        }
    }
}

2.UI坐标 UI坐标的起点位置是屏幕中心点如果想实现一个鼠标点击到哪图片就出现在哪需要将屏幕坐标Input.mousePosition转化为UI坐标。比如屏幕坐标值是Screen.width/2Screen.height/2在UI坐标里正好是0

public class UIClick : MonoBehaviour
{
    public Transform img; //要改变位置的UI

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            Vector2 pos = Input.mousePosition;
            Debug.Log("screen w:" + Screen.width + ", h:" + Screen.height);
            Debug.Log("click pos x:" + pos.x + ",pos y:" + pos.y);

            float X = Input.mousePosition.x - Screen.width / 2f;
            float Y = Input.mousePosition.y - Screen.height / 2f;
            Vector2 tranPos = new Vector2(X, Y);
            img.localPosition = tranPos;
        }
    }
}

上面这个代码,运行起来后,切换屏幕分辨率,就会发现错位了:

!Pasted image 20230405195750.png

这是因为当前的Canvas做了屏幕适配

!Pasted image 20230405195802.png

可以使用sizeDelta得到缩放后的尺寸

public class UIClick : MonoBehaviour
{
    public Transform img; //要改变位置的UI

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            Vector2 pos = Input.mousePosition;
            Debug.Log("screen w:" + Screen.width + ", h:" + Screen.height);
            Debug.Log("click pos x:" + pos.x + ",pos y:" + pos.y);

            float X = Input.mousePosition.x - Screen.width / 2f;
            float Y = Input.mousePosition.y - Screen.height / 2f;
            Vector2 tranPos = new Vector2(X, Y);
            img.localPosition = tranPos;

            //得到画布的尺寸
            Vector2 uisize = this.GetComponent<RectTransform>().sizeDelta;
            Debug.Log("sizeDelta w:" + uisize.x + ", h:" + uisize.y);
        }
    }
}

当前分辨率为1280 * 3000点击屏幕右上角得到如下数据

!Pasted image 20230405195859.png

按照比例缩放即可: 缩放后的坐标除以缩放前的坐标=缩放后的屏幕尺寸除以缩放前的屏幕尺寸=uisize.x/Screen.width 变形一下得到最终位置:

public class UIClick : MonoBehaviour
{
    public Transform img; //要改变位置的UI

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            Vector2 pos = Input.mousePosition;
            Debug.Log("screen w:" + Screen.width + ", h:" + Screen.height);
            Debug.Log("click pos x:" + pos.x + ",pos y:" + pos.y);

            float X = Input.mousePosition.x - Screen.width / 2f;
            float Y = Input.mousePosition.y - Screen.height / 2f;
            Vector2 tranPos = new Vector2(X, Y);
            img.localPosition = tranPos;

            //得到画布的尺寸
            Vector2 uisize = this.GetComponent<RectTransform>().sizeDelta;
            Debug.Log("sizeDelta w:" + uisize.x + ", h:" + uisize.y);

            Vector2 finalPos = new Vector2(X * (uisize.x / Screen.width), Y * (uisize.y / Screen.height));
            img.localPosition = finalPos;
        }
    }
}

二、方式二

public class UIClick : MonoBehaviour
{
    public Transform img; //要改变位置的UI

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            //屏幕坐标转换世界坐标
            var camera = this.GetComponent<Canvas>().worldCamera;
            Vector3 worldPos = camera.ScreenToWorldPoint(Input.mousePosition);
            //世界坐标转换位本地坐标
            Vector2 uiPos = this.transform.InverseTransformPoint(worldPos);
            img.localPosition = uiPos;
        }
    }
}

三、方式三

RectTransformUtility.ScreenPointToLocalPointInRectangle(
RectTransform rect,
Vector2 screenPoint,
Camera cam,
out Vector2 localPoint);

参数 rect

这个参数需要你提供一个父物体的RectTransform。因为这个方法是取得UI坐标而UI坐标都是局部坐标所以一定需要一个父物体才能计算出局部坐标。(有父物体才有局部坐标对吧!)

最后,这个方法就会把屏幕上的点转化为这个父物体下的局部坐标。

参数 screenPoint

这个参数需要你提供一个屏幕空间 的坐标 (屏幕坐标)。

最后,这个方法会把这个屏幕坐标 转化为ui坐标。

参数 cam

这个参数需要你指定一个相机。

因为UI坐标是根据相机来确定的。(如果Canvas是Screen Space-overlay模式cam参数应为null)

参数 localPoint

最后这个方法会把屏幕点的UI坐标的结果装到这个变量中。

你也可以这样理解你给指定一个Vector2变量给这个参数这个方法最后会把转换好的UI坐标赋值给你指定的这个变量。

返回值 - bool类型

这个方法有一个返回值,但是我自己没使用过这个返回值(感觉用处不大?)。

官方的说法是这个返回值是判断此点是否在Rect所在的平面上。

如果在就返回true。

public class UIClick : MonoBehaviour
{
    public Transform img; //要改变位置的UI

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            var camera = this.GetComponent<Canvas>().worldCamera;
            Vector2 uipos = Vector3.one;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                this.GetComponent<RectTransform>(), Input.mousePosition, camera, out uipos);
            img.localPosition = uipos;
        }
    }
}