Layout Group

Vertical

  • 竖直布局

Horizontal

  • 水平布局

UI_HorizontalLayoutGroupInspector

Grid

  • 网格布局

Layout Element

  • 布局元素

UI_LayoutElementInspector

Content Size Fitter

  • 自适应

应用

  • 主要在外界有布局的情况下,子物体还需要自适应等各种情况
  • 看情况Control Child Size,在外界已经挂载Content Size Fitter的情况下,子物体再挂载是无效的,默认子物体都是自适应长度的,对于不需要变化的或者特殊的处理,只需要挂Layout Element,对其进行设置即可

Pivot

  • 轴心
  • 父物体轴心位置决定子物体生成的位置
    • 0.5 在中心依次向右,中心一直变
    • 0 左边定住,第一个不变,每增加一个都向右移动
    • 1 右边定住,每增加一个,前面的都向左移动

RectTransform

动态改变布局

stretch

下载

1
2
3
4
5
6
7
8
//改变RectTransform的top
GetComponent<RectTransform>().offsetMax = new Vector2(GetComponent<RectTransform>().offsetMax.x, top);

//改变RectTransform的bottom
GetComponent<RectTransform>().offsetMin = new Vector2(GetComponent<RectTransform>().offsetMin.x, bottom);

//改变RectTransform的width,height
GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);

middle center

center

1
2
3
//改变RectTransform的pos
GetComponent<RectTransform>().anchoredPosition3D = new Vector3(posx,posy,posz);
GetComponent<RectTransform>().anchoredPosition = new Vector2(posx,posy);

拼UI时注意

  • 外部背景等最好用stretch去适应外界边框,对于相对于外界边框变化而需要变化的实用stretch
  • 内部用center,直接固定好相对位置

UI坐标转换,安全区处理

https://blog.csdn.net/qq_42672770/article/details/123033379?spm=1001.2014.3001.5501

https://cloud.tencent.com/developer/article/1601274

var screenPos = RectTransformUtility.WorldToScreenPoint(GameCameraMgr.Instance.UICamera, gameObject.transform.position)

这样做更好

Device Simulator

  • 设备模拟器:模拟不同屏幕下的自适应情况,Window => General => Device Simulator
  • 搭完UI看一下不同设备,或者设置极端分辨率,例如正方形和最扁的情况

UI文本框

  • UI文本框一般情况下也要长一点
  • Text框和外层的边框大小要一样
  • 注意设置居中