Unity的界面排版: RectTransform
看Unity3D文档像看国内教课书一样,一些概念,不懂的时候看还是不懂,明白了以后再看,好像也没有说错。好几个做Unity3D的朋友跟我吐槽过U3D的文档质量,相比Apple贴心的技术文档相去甚远。
话虽这么说,权威的资料当然还是官方的,建议先仔细读一遍,不懂的再往下读:
http://docs.unity3d.com/Manual/UISystem.html
网上的教程大多泛泛而谈,就不吐槽了。好在还找到了一篇比较好的解读,在此谢谢作者白猫。文章链接在下面
http://www.cnblogs.com/whitecat/p/4159815.html
uGUI里面AutoLayout比较特别,我就此详细描述一下,希望能减少“教科书”的理解障碍。
RectTransform的理解
UI元素有专门的RectTransform组件来描述元素的几何信息,继承于Transform,Inspector中其属性如下图。
Anchors,锚点
我们首先需要认识Anchors。官网上的图片很好表达了Andhors的功能,请恕我实践下拿来主义。
善于观察的同学可能已经发现,不管什么情况,button的四个顶点到相应锚点的相对位置是不变的:
当4个锚点都在一起的时候,RectTransform会显示Pos X,Pos Y,Width,Height四个属性可供修改。
当不在一起的时候,RectTransform可调整的属性会有变化。
Anchors的Min和Max分别是正规化的值(从0到1),表示占父RectTransform的百分比,下图中AnchorMin=(0.1,0.1) AnchorMax=(0.9,0.9)
Pivot,UI的中心点
UI元素的旋转和缩放是围绕pivot进行的。RectTransform组件中,Pivot属性是一个正规化的二维向量,用来描述中心点在本身矩形大小的位置。默认值为(0.5, 0.5),即几何中心。
以上是RectTransform可视化的编辑属性,实际上RectTransform类的属性是怎样的?查阅RectTransform的官方文档如下:
anchoredPosition
The position of the pivot of this RectTransform relative to the anchor reference point.
anchoredPosition3D
The 3D position of the pivot of this RectTransform relative to the anchor reference point.
anchorMax
The normalized position in the parent RectTransform that the upper right corner is anchored to.
anchorMin
The normalized position in the parent RectTransform that the lower left corner is anchored to.
offsetMax
The offset of the upper right corner of the rectangle relative to the upper right anchor.
offsetMin
The offset of the lower left corner of the rectangle relative to the lower left anchor.
pivot
The normalized position in this RectTransform that it rotates around.
rect
The calculated rectangle in the local space of the Transform.
sizeDelta
The size of this RectTransform relative to the distances between the anchors.
乍一看有点费解,这里有必要解释下:
anchoredPosition
可以理解为Pivot点相对于Anchor reference点的位置。Anchor reference点,我是这样理解的:当四个anchors点在一起的时候,这个点就是anchor reference点;否则这四个点的中心点就是anchor reference点。
照这个推理,anchorMax和anchorMin的值将影响anchoredPosition的值。具体还需要demo研究来确认。
但可以确定的是,当一个UI元素不需要自动拉伸行为时,用anchoredPosition + sizeDelta来设置位置和大小是比较方便的方法。
anchorMax、anchorMin和Inspector中的意义一致,需要注意的是,当UI元素不需要自动拉伸时,AnchorMax和AnchorMin是相等的。
UI元素需要自动拉伸时,使用anchorMax、anchorMin + offsetMax、offsetMin来设置UI的位置及大小会比较方便。
其中,anchorMax.x == anchorMin.x,height会自动拉伸,width固定不变。
anchorMax.y == anchorMin.y,width会自动拉伸,height固定不变。
不知不觉已过凌晨1点,先介绍到这里。关于uGUI的更多内容,将在后续博文中继续,敬请期待。
Unity的界面排版: RectTransform的更多相关文章
- Unity学习笔记(4) --- Unity的界面排版:初识GUI
GUI和GUILayout是Unity提供的UIKit.在使用GUI的Controls时都要求设置Rect參数.没办法做到自己主动排版,给适配带来难度.而GUILayout的设计就是为了弥补这个缺陷, ...
- Unity编辑器 - 自动排版
Unity编辑器 - 自动排版 使用花括号提高可读性 //一组横向排列的控件 GUILayout.BeginHorizontal(); { GUILayout.BeginVertical(); { / ...
- Android 界面排版的5种方式
Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对 ...
- Unity 屏幕坐标到UGUI RectTransform本地坐标的转换
public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camer ...
- 界面排版-TableLayout的stretchColumns方法
1.先把XML內的預設RelativeLayout排版方式清空,在去Layout區拉一個TableLayout的到表單上,XML下會出現下面程式碼 <TableLayout xmln ...
- html+css+js整体布局——[防止浏览器扩大,界面排版混乱]
1,body——>width:100% body { background-color: rgb(238, 238, 238); color: rgb(51, 51, 51); display: ...
- unity,set ugui rectTransform anchor by script
如果想用代码实现与下面面板相同的功能 试验可知改变上面选项下面四个值也随之变化: 所以说明二者是一回事儿. 因此,只要通过代码修改RectTransform的anchorMax和anchorMin成员 ...
- [unity]UGUI界面滑动,ScrollRect嵌套滑动
原因:老板蛋痛,让我去抄皇室战争. 思路:我大概知道ngui(后来改成UGUI的)里面有个ScrollView.于是我就想一个横着的SV加上5个竖的SV不就好了吗. 过程: 于是 但是有个问题就是UI ...
- ubuntu14.04安装bodhi桌面系统后,unity启动界面改变,如何还原
按一下 ctrl + alt + f1 # 出現 tty11. sudo service lxdm stop或sudo /etc/init.d/lxdm stop如果出現錯誤訊息 不理它 繼續2. s ...
随机推荐
- codis+redis集群学习整理(待续)
Codis 由四部分组成: Codis Proxy (codis-proxy) Codis Manager (codis-config) Codis Redis (codis-server) ZooK ...
- Asp.net Vnext 模块化实现
概述 本文已经同步到<Asp.net Vnext 系列教程 >中] 在程序中实现模块化可以加快开发效率,通过替换模块实现升级. 架构 vnext 没有 Virtualpathprovide ...
- BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14982 Solved: 6081[Submit ...
- Educational Codeforces Round 9 D - Longest Subsequence
D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...
- 修改linux系统编码
方法1: vi /etc/sysconfig/i18n 默认为: LANG="en_US.UTF-8"SYSFONT="latarcyrheb-sun16" ...
- 【Java】java.lang.NullPointerException的两个原因
自己写程序时出现过 java.lang.NullPointerException错误的一些原因 (1)自己建立了一个数组,但每个数组元素没单独初始化(单独new一个新对象). (2)在进行if (ar ...
- OneDrive 开机启动设置失效如何处理?
问题现象: 『设置里勾选了开机启动onedrive,但是重启电脑勾选就没了,重新勾选没用,一重启就没了』 win10的onedrive无法开机启动 - Microsoft Community 处理方式 ...
- 基于spring-boot的应用程序的单元测试方案
概述 本文主要介绍如何对基于spring-boot的web应用编写单元测试.集成测试的代码. 此类应用的架构图一般如下所示: 我们项目的程序,对应到上图中的web应用部分.这部分一般分为Control ...
- TCP 建立连接为什么要握 3 次手?
上次已经说过,没有协议,不成方圆,计算机之间的通信更是依赖于协议.今天就重点分析一下 TCP 协议. 传输控制协议 TCP 是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 7 ...
- 一个UICollectionView自定义layout的实现
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @ ...