Unity各坐标转化

引用

https://blog.csdn.net/LLLLL__/article/details/88124564

各个坐标


世界坐标:世界中的坐标(transform.position)

局部坐标:相对父物体的坐标(transform.localPosition)

屏幕坐标:屏幕的分辨率有关(z轴的坐标为相机的世界坐标轴的负值)

视口坐标:屏幕坐标系单位化(z轴的坐标为相机的世界坐标轴的负值)

GUI坐标:二维坐标(左上角为(0,0),右下角为(Screen.Width,Screen.height))


各种坐标的转换


        世界和局部坐标的转换

世界坐标转换为局部坐标:如计算物体B相对于物体A的局部坐标,A.transform.InverseTransformPoint(B)

局部坐标转换为世界坐标:只能转换子物体的位置到世界坐标,transform.TransformPoint(B.localPosition)

        屏幕坐标和世界转换

获得鼠标在屏幕坐标系中的坐标:Input.mousePosition

屏幕坐标转换到世界坐标:Camera.ScreenToWorldPoint(Vector3 position)

世界坐标转换到屏幕坐标:Camera.WorldToScreenPoint(Vector3 position)

        屏幕坐标和视口坐标

屏幕坐标转换成视口坐标:  Camera.ScreenToViewportPoint(Vector3 position)

视口坐标转换成屏幕坐标:Camera.ViewportToScreenPoint(Vector3 position)

        世界坐标和视口坐标

世界坐标转换成视口坐标:Camera.WorldToViewportPoint(Vector3 position)

视口坐标转换成世界坐标:Camera.ViewportToWorldPoint(Vector3 position)

        UI坐标和屏幕坐标

屏幕坐标转换成UI局部坐标:
RectTransformUtility.ScreenPointToLocalPointInRectangle(RectTransform rect,Vector2 screenPos,Camera uiCamera,out Vector2 uiLocalPos)

UI世界坐标转换成屏幕坐标:
RectTransformUtility.WorldToScreenPoint(Camera uiCamera,Vector3 worldPos)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值