標籤:style blog http color os io for 2014
如所示:一個Grid下面有六個Button,它們需要在不同的分辨下展開適應(Horizontal)寬度,以保證填充滿底部
首先有這兩個要點:
1、UIGrid中的Cell Width是根據螢幕比例動態調整的
2、NGUI的UICamera有一個onScreenResize 委託
我的布局如下:
1、首先Grid下有六個子Button,Grid的參數設定如下:Cell Width是根據我的圖片的大小,這裡設定個大概值就好,因為不同解析度,我們需要動態調整這個值
2、每個子Child即Button都綁定上UIStretch指令碼,並把Style設定為Horizontal(水平),其中的Relative Size=1/6 ~=0.16667
3、綁定GridTest指令碼在Grid上:
using UnityEngine;using System.Collections;public class GridTest : CUIBase{ UIGrid btnsGrid; // Use this for initialization void Start() { btnsGrid = (UIGrid)GetControl<UIGrid>("BtnsGrid"); UIWidget _widget = GetControl<UIWidget>("BtnsGrid/Btn01HomeBtn"); btnsGrid.cellWidth = _widget.width; btnsGrid.Reposition(); UICamera.onScreenResize += ScreenSizeChanged; } // Update is called once per frame void Update() { } void ScreenSizeChanged() { UIWidget _widget = GetControl<UIWidget>("BtnsGrid/Btn01HomeBtn"); btnsGrid.cellWidth = _widget.width; btnsGrid.Reposition();//Grid重新進行排序 CBase.Log("size change"); }}
4、點擊Play,修改螢幕解析度,我們可以看到在不同的分辨下,這六個Button都可以完全填充底部地區。