UIRoot脚本绑定的GameObject是NGUI里所有UI对象的根节点,脚本本身的作用是将根节点保持缩放到2/(Screen.height)的大小。
由于Unity中对父对象进行缩放会同时对其子对象做相应的缩放,因此NGUI用UIRoot控制对整个UI的缩放以适配不同屏幕的宽高比。
UIRoot有3种缩放模式:
public enum Scaling
{
PixelPerfect,
FixedSize,
FixedSizeOnMobiles,
}
PixelPerfect模式是当屏幕高度在minimumHeight和maximumHeight区间时不进行缩放。
FixedSize模式是只有屏幕高度为manualHeight时才不进行缩放。
FixedSizeOnMobiles模式与FixedSize模式无异,只是通过UNITY_IPHONE和UNITY_ANDROID宏区分该模式是否能生效。
参考文章:Unity3D开发(一):NGUI之UIRoot屏幕分辨率自适应
不同屏幕高度下的缩放比可以通过GetPixelSizeAdjustment函数获取:
public float GetPixelSizeAdjustment (int height)
{
height = Mathf.Max(2, height);
if (scalingStyle == Scaling.FixedSize)
return (float)manualHeight / height;
#if UNITY_IPHONE || UNITY_ANDROID
if (scalingStyle == Scaling.FixedSizeOnMobiles)
return (float)manualHeight / height;
#endif
if (height < minimumHeight) return (float)minimumHeight / height;
if (height > maximumHeight) return (float)maximumHeight / height;
return 1f;
}
缩放后的高度应为:Resource’s height / GetPixelSizeAdjustment(Screen.height)
UIRoot中有一个静态列表用于管理处于enable状态下的所有UIRoot对象,并提供了Broadcast方法。