unity ugui 坐标位置总结
来源:互联网 发布:达芬奇 知乎 编辑:程序博客网 时间:2024/05/18 13:04
Ugui中的transform下有两个位置一个是position,一个是localposition
可以简单理解position是世界坐标(位于空间下的某个位置)
Localposition是相对位置是相对与父节点的偏移位置
这些和模型是一个道理
白色图片对应Image
红色图片对应Image3
两张图片的宽高都是100,两个图片的x距离是100个单位(只用x距离观察,y距离同理)
Image(白色图片)属性
Image2(红色图片父节点)属性
Image(红色图片)属性
Game视图下的分辨率是模拟的具体游戏运行时的屏幕分辨率
代码中的获取方式:Screen.width 和 Screen.height
Canvas Scaler中的UI Scale Mode我们一般会选用Scale with Screen的模式,本文中将ReferenceResolution 设置为1280*720
Canvas.GetComponent<CanvasScaler>().referenceResolution此方法可以获得这个值
一.Canvas的Render Mode有三种模式,如果项目中没有特殊需求会选择第一种Screen Space-Overlay
1. 如果此时屏幕分辨率也是1280*720
那么image3.position.x-image.position.x = 100
image3.localposition.x-image.localposition.x= 0
2. 如果此时屏幕分辨率变为:1980*1080
image3.position.x-image.position.x = 150
image3.localposition.x-image.localposition.x= 0
显然如果按照这个距离去做一些ugui位置的处理是不正确的
这个时候我们就需要Canvas.GetComponent<CanvasScaler>().referenceResolution与Screen.width 和Screen.height的比例来确定具体的位置
另外通过RectTransformUtility.ScreenPointToLocalPointInRectangle方法来获取的坐标依然可以正确确定二者的位置
二,很多时候为了显示界面特效,需要引入camera,Render Mode选择了Screen Space-Camera的模式,这个时候
image3.position.x-image.position.x = 16
image3.localposition.x-image.localposition.x = 0
这个时候使用RectTransformUtility.ScreenPointToLocalPointInRectangle转换后的坐标
Vector2 pos;
Vector2 pos3;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,image3.transform.position, canvas.GetComponent<Camera>(), out pos3))
{
Debug.Log(pos3);
}
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,image.transform.position, canvas.GetComponent<Camera>(), out pos))
{
Debug.Log(pos);
}
pos3.x-pos.x 依然等于100
当然也可以得到canvas里设置的camera
Vector3 pos =camera.WorldToScreenPoint(image.transform.position);
Vector3 pos3 =camera.WorldToScreenPoint(image3.transform.position);
将世界坐标position转换为屏幕坐标
Pos3.x-pos.x依然等于100
如果此时实际屏幕分辨率为 1980*1080
那么RectTransformUtility.ScreenPointToLocalPointInRectangle获得的坐标距离依然是100
而camera.WorldToScreenPoint转换以后还需要通过Canvas.GetComponent<CanvasScaler>().referenceResolution与Screen.width 和 Screen.height的比例来确定具体的位置
- unity ugui 坐标位置总结
- Unity Ugui射线坐标转换总结
- Unity UGUI坐标转为3D世界坐标 世界左边转为UGUI坐标
- unity UGUI scrollview 置顶拖拽位置
- unity获取ugui上鼠标位置
- Unity UGUI之Canvas总结
- cocos2d坐标位置总结
- Unity UGUI的适配方法总结
- Unity的UGUI调节深度而不影响位置
- unity中UGUI图片箭头指向鼠标位置
- Unity的UGUI调节层次而不影响位置
- Unity UGUI
- 【Unity&UGUI】
- Unity3D+kinect2 实现关节位置到UGUI坐标的转换,以及判断是否在某个UGUI中
- Qt获取控件位置,坐标总结
- Qt获取控件位置,坐标总结
- Qt获取控件位置,坐标总结
- js里面关于位置坐标的总结
- 1.3 输出第二个整数
- Stars poj 2352 treap
- BZOJ3612 [Heoi2014]平衡 递推 整数划分
- NYOJ A : 绝望的riba2534
- PCI-E 1X金手指封装
- unity ugui 坐标位置总结
- volatile关键字
- 训练日记-39
- 数据库隔离级别
- 11月16日
- 学学Gradle之一
- C#基础(19)——path、File类的基本操作
- 1129. Recommendation System (25)
- javascript 函数