UGUI自适应

用UGUI作东西的时候,自适应选择scalewithscreensize,默认是基于高度进行等比缩放,参见上一篇NGUI自适应,可是UGUI有个叫作Anchor的东西,即当前图片相对于父节点的位置,将anchor的四个角与本身的四个角关联在一块儿,既能够实现非等比缩放。即物体的大小就等于四个anchor所造成的区域,通常是屏幕的百分比。若是要使屏幕在宽高比低于某个标准值的时候表现为顶部和底部出现黑边,大于标准值横向拉伸,能够在Canvas下面添加一个panel,动态的去改变该panel的大小便可。this

private readonly float _refWidth = 960.0f;
private readonly float _refHeight = 640.0f;
private readonly float _refRatio = 960.0f / 640.0f;

// Use this for initialization
void Start ()
{
if (Screen.width * 1.0f / Screen.height > _refRatio)
{
GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width / (Screen.height / _refHeight), _refHeight);
}
else
{
GetComponent<RectTransform>().sizeDelta = new Vector2(_refWidth, _refHeight);
}
}
相关文章
相关标签/搜索