localPosition:表示的是子物體的pivot相對(duì)于父物體RectTransform的中心點(diǎn)pivot的相對(duì)坐標(biāo)茫孔。
anchoredPosition3D:表示的是子物體的pivot相對(duì)于錨點(diǎn)anchor的相對(duì)坐標(biāo)叮喳。
anchorMax:右上方錨點(diǎn)位置(所在位置與父物體RectTransform寬度和長(zhǎng)度的比例)
anchorMin:左下方錨點(diǎn)位置(所在位置與父物體RectTransform寬度和長(zhǎng)度的比例)
offsetMax:從右上方錨點(diǎn)位置到UI右上方坐標(biāo)的偏移向量
offsetMin:從左下方錨點(diǎn)位置到UI左下方坐標(biāo)的偏移向量
sizeDelta:offsetMax - offsetMin
rect:RectTransform中的屬性,不與UI元素所在的位置有關(guān)
SetSizeWithCurrentAnchors(Animations.Axis axis, float size):可以通過直接設(shè)置rect中的width和height值來改變UI元素的大小缰贝。
SetInsetAndSizeFromParentEdge(RectTransform.Edge edge, float inset, float size):根據(jù)父物體的Edge(某一邊)去布局
寫了個(gè)編輯器拓展馍悟,選中g(shù)ameObject,按快捷鍵會(huì)輸出一些RectTransform的信息剩晴。
using UnityEngine;
using UnityEditor;
public class TestEditor
{
[MenuItem("MyTool/ShowRectTransform", true)]
private static bool Validate()
{
if (Selection.objects.Length > 0)
return true;
else
return false;
}
[MenuItem("MyTool/ShowRectTransform %_q", false)]
private static void MyToolShowRectTransform()
{
GameObject go;
//Selection.objects 返回場(chǎng)景或者Project中選擇的多個(gè)對(duì)象
foreach (Object item in Selection.objects)
{
go = ((GameObject)item);
RectTransform rect = go.GetComponent<RectTransform>();
Debug.Log(go.name + ".localPosition=====" + rect.localPosition);
Debug.Log(go.name + ".anchoredPosition3D=====" + rect.anchoredPosition3D);
Debug.Log(go.name + ".anchorMax=====" + rect.anchorMax);
Debug.Log(go.name + ".anchorMin=====" + rect.anchorMin);
Debug.Log(go.name + ".offsetMax=====" + rect.offsetMax);
Debug.Log(go.name + ".offsetMin=====" + rect.offsetMin);
Debug.Log(go.name + ".sizeDelta=====" + rect.sizeDelta);
Debug.Log(go.name + ".rect=====" + rect.rect);
//rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);
//rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 622);
}
}
}
1. localPosition與anchoredPosition3D(anchoredPosition)
localPosition表示的是子物體的pivot相對(duì)于父物體RectTransform的中心點(diǎn)pivot的相對(duì)坐標(biāo)锣咒。
anchoredPosition3D表示的是子物體的pivot相對(duì)于錨點(diǎn)anchor的相對(duì)坐標(biāo)。
也就是說赞弥,當(dāng)錨點(diǎn)與父物體RectTransform的中心點(diǎn)重合時(shí)毅整,localPosition與anchoredPosition相當(dāng)。
可以看到兩者相等绽左,如果移動(dòng)錨點(diǎn)位置悼嫉,則只改變anchoredPosition。
如果將錨點(diǎn)打開拼窥,則anchoredPosition為中心點(diǎn)到錨點(diǎn)正中央的位置相對(duì)坐標(biāo)戏蔑。
2. offsetMax與offsetMin
offsetMax:從右上方錨點(diǎn)位置到UI右上方坐標(biāo)的偏移向量
offsetMin:從左下方錨點(diǎn)位置到UI左下方坐標(biāo)的偏移向量
當(dāng)錨點(diǎn)沒有打開時(shí):
當(dāng)錨點(diǎn)打開時(shí):
offsetMax與offsetMin可以修改,當(dāng)錨點(diǎn)打開到最大時(shí)鲁纠,可以用來動(dòng)態(tài)調(diào)整子物體到邊界的距離辛臊。
3.sizeDelta
sizeDelta=offsetMax - offsetMin
在錨點(diǎn)沒打開的時(shí)候,就是子物體的長(zhǎng)和寬,可以用來動(dòng)態(tài)修改長(zhǎng)寬房交。
打開的時(shí)候彻舰,就是錨點(diǎn)的框框長(zhǎng)寬跟子物體Rect長(zhǎng)寬的差值。
4. rect(只讀)
rect中的屬性候味,不與UI元素所在的位置有關(guān)刃唤,只和其自身屬性相關(guān),所以其中的rect.width和rect.height屬性就可以讓我們?cè)谌魏吻闆r下取得子物體的大小白群,而rect.x和rect.y表示的是以Pivot為原點(diǎn)尚胞,UI元素左下角的坐標(biāo):
5.SetSizeWithCurrentAnchors(Animations.Axis axis, float size)
可以通過直接設(shè)置rect中的width和height值來改變UI元素的大小。
需要強(qiáng)調(diào)的是帜慢,在動(dòng)態(tài)設(shè)置width或者length時(shí)笼裳,由于PosX,PosY不變粱玲,所以全看中心點(diǎn)設(shè)在哪里躬柬,設(shè)在最左邊,那么只會(huì)往右拉寬抽减,設(shè)在最上面允青,只會(huì)往下拉寬。
縮短width
縮短width
6.SetInsetAndSizeFromParentEdge(RectTransform.Edge edge, float inset, float size)
根據(jù)父物體的Edge(某一邊)去布局
rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 622);
說的是卵沉,讓子物體距離右邊長(zhǎng)度為零颠锉,設(shè)置其寬度為622法牲。