当前位置: 代码网 > it编程>游戏开发>unity > unity中屏幕坐标转UI坐标

unity中屏幕坐标转UI坐标

2024年08月06日 unity 我要评论
/ 如果Canvas的渲染模式为Screen Space - Overlay,Camera设置为null。// 目标UI父物体的RectTransform。// 获取鼠标位置(屏幕坐标)Debug.Log("UI坐标: " + localPoint);// 将屏幕坐标转换为UI坐标。

using unityengine;  
using unityengine.eventsystems;  
  
public class screentouiconverter : monobehaviour  
{  
    public recttransform targetparentrect; // 目标ui父物体的recttransform  
  
    void update()  
    {  
        vector2 mousepos = input.mouseposition; // 获取鼠标位置(屏幕坐标)  
        camera canvascam = null; // 如果canvas的渲染模式为screen space - overlay,camera设置为null  
  
        // 将屏幕坐标转换为ui坐标  
        vector2 localpoint;  
        if (recttransformutility.screenpointtolocalpointinrectangle(targetparentrect, mousepos, canvascam, out localpoint))  
        {  
            debug.log("ui坐标: " + localpoint);  
        }  
    }  
}

另一种:
                // 将屏幕坐标转换为世界坐标
                vector3 worldpoint = mcamera.screentoworldpoint(new vector3(screenpoint.x, screenpoint.y, mcamera.nearclipplane));

                // 将世界坐标转换为本地坐标
                vector3 localpoint = root.transform.inversetransformpoint(worldpoint);

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com