【小松教你手遊開發】【unity實用技能】NGUI Scrollview的Reposition的幾個總結

chrisfxs發表於2015-11-26

NGUI的Scrollview當要重置的時候那幾個reposition阿,ResetPosition,repositionNow什麼的非常煩。

現在稍稍總結一下

首先。分清兩個概念。一個是UIScrollview,一個是UIGrid。


UIScrollview是負責顯示用的,UIGrid會在UIScrollview下。移動啊,置頂啊,其實都是UIScrollview控制的。

Scrollview.ResetPosition()就是讓這個表格回到頂端(預設頂端,可以自己設定)


UIGrid是負責把所有資料放到UIGrid這個框框裡的。操作只跟自己框框內的有關,所有這個UIScrollView的移動跟這個沒關係。

所以grid.Reposition()只是讓格子裡的資料重新排布一下。

grid.repositionNow = true;是讓格子立刻重新整理。

(在使用過程中發現只有下面的能用。看裡面也沒懂為什麼。。。)


所以說當你要刪除資料中的一條時,就是把那個GameObject刪掉。

然後grid.repositionNow = true;讓UIGrid裡面下面的資料網上推

然後scrollview.ResetPosition();//讓Scrollview重新刷一下回到頂端。


但是如果不需要去到頂端就不用scrollview.ResetPosition();了。


但是這個會有一個問題就是grid.keepWithinPanel = true會告訴UIScrollview這些資料需要顯示在框框裡,但是你又在移裡面的位置。這時候就會出問題。

所以解決的方法是暫時設為false。


所以總程式碼如下:

        //Update grid
        bool originalKeepWithinPanel = _itemGrid.keepWithinPanel;
        _itemGrid.keepWithinPanel = false;  //Need to set this to false or else buttons will not properly be at top if player scrolls past bottom then switch tabs
        _itemGrid.repositionNow = true;
        //_scrollView.ResetPosition(); //Reset and move scroll panel back to original start position, required before and after tabbing - see ngui docs
        _itemGrid.keepWithinPanel = originalKeepWithinPanel;


 

相關文章