我希望我的RectTransform看起来像这样:
我正在尝试:
m_Viewport = go.GetComponent<RectTransform>();
m_Viewport.anchorMin = Vector2.zero;
m_Viewport.anchorMax = Vector2.one;
m_Viewport.localPosition = Vector3.zero;
m_Viewport.localScale = Vector3.one;
m_Viewport.offsetMin = new Vector2(30, 0);
m_Viewport.offsetMax = new Vector2(0, 0);
m_Viewport.sizeDelta = new Vector2(-20, 0);
但结果如下:
答案 0 :(得分:5)
尝试一下:
float left = 30;
float right = 20;
m_Viewport.anchorMin = Vector3.zero;
m_Viewport.anchorMax = Vector3.one;
m_Viewport.anchoredPosition = new Vector2((left - right)/2, 0f);
m_Viewport.sizeDelta = new Vector2(-(left + right), 0);
sizeDelta
将rectTransform按相同比例向左右拉伸。如果您希望左侧尺寸与右侧尺寸不同,则必须移动x位置进行调整。