UIGrid+自適應

來源:互聯網
上載者:User

標籤: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都可以完全填充底部地區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.