1、拉伸铺满
RectTransform rect = GetComponent<RectTransform>();rect.anchorMin = Vector2.zero;rect.anchorMax = Vector2.one;rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width);rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Screen.height);
2、向左居中对齐
RectTransform rect = GetComponent<RectTransform>(); rect .anchorMin = new Vector2(0, 0.5f);rect .anchorMax = new Vector2(0, 0.5f);rect .pivot = new Vector2(0, 0.5f);
3、修改大小并保持位置不变
RectTransform rect = GetComponent<RectTransform>(); rect .anchorMin = new Vector2(0, 0.5f);rect .anchorMax = new Vector2(0, 0.5f);rect .pivot = new Vector2(0, 0.5f);Vector3 originalPos = rect .localPosition;rect .sizeDelta += new Vector2(40, 0);rect .localPosition = originalPos new Vector3(40f / 2, 0);