AspectRatioFitter类

调整RectTransform大小,以适应指定的宽高比。继承了UIBehaviour, ILayoutSelfController。

属性:

aspectMode 执行宽高比的模式

aspectRatio 应用的宽高比,宽度除以高度

OnEnable方法

调用SetDirty(true),启动协程延时更新,延时后调用UpdateRect

OnDisable方法

调用LayoutRebuilder.MarkLayoutForRebuild,重建Layout。

OnRectTransformDimensionsChange方法

RectTransform大小发生变化时,调用UpdateRect。

UpdateRect方法

  • 在编辑器模式下,没有运行的状态,默认AspectMode.None时,限制宽高比(RectTransform宽度除以高度)在0.001到1之间。
  • 当选择模式AspectMode.HeightControlsWidth(以高度为基准,控制宽度),调用 rectTransform.SetSizeWithCurrentAnchors方法,在水平轴上,以rectTransform.rect.height * m_AspectRatio设置宽度的值。
  • 当选择模式AspectMode.WidthControlsHeight(以宽度为基准,控制高度),调用 rectTransform.SetSizeWithCurrentAnchors方法,在垂直轴上,以rectTransform.rect.width / m_AspectRatio设置高度的值。
  • 当选择模式AspectMode.FitInParent时,当aspectRatio比例小于父对象的宽高比,以父对象的宽度以基准,按照aspectRatio设置高度。当aspectRatio比例大于父对象的宽高比,以父对象的高度以基准,按照aspectRatio设置宽度。
  • 当选择模式AspectMode.EnvelopeParent时,当aspectRatio比例小于父对象的宽高比,以父对象的高度以基准,按照aspectRatio设置宽度。当aspectRatio比例大于父对象的宽高比,以父对象的宽度以基准,按照aspectRatio设置高度。