2017-10-07 34 views
0

我有一個gameobject1(加入Scroll Rect組分)和它的另一個內部gameobject2(該Scroll rect組件的內容)。在gameobject2具有圖像images.The數量可以是10或20 ..(任何數量)。該Movement TypeElastic。如你所知,它將停止滾動,直到gameobject2身高的長度。如何停止動態數字的長度。在gameobject2圖像的數量可以不同。這取決於搜索結果。結果可以是5,8或200.所以我需要滾動,直到搜索結果的最後。所以如何停止滾動精確的長度在Scroll rect組件?如何創建動態停止統一滾動矩形?

回答

0

您可以使用ContentSizeFitter組件。名稱爲「Content」的GameObject是「ScrollView」-gameObject的scrollRect組件的內容。

Scroll rect's content

Image

0

RectTransform#SetSizeWithCurrentAnchors

建立動態滾動列表時,我用這個有很多。在添加了我想要的所有項目(並且每個項目都具有已知大小,並且都使用該大小定位)後,我使用新大小(添加的對象總數*對象大小)更新了contentRectTransform

例如,我有這樣的代碼:

int i = 0; 
//for each item in a list of skills... 
IEnumerator<Skill> list = SkillList.getSkillList(); 
Transform skillListParent = GuiManager.instance.skillPanel.transform; 
while(list.MoveNext()) { 
    Skill sk = list.Current; 
    //create a prefab clone... 
    GameObject go = Main.Instantiate(PrefabManager.instance.SKILL_LISTITEM, skillListParent) as GameObject; 
    //set its position... 
    go.transform.localPosition = new Vector3(5, i * -110 -5, 5); 
    //add a button event or other data (some lines omitted)... 
    Transform t1 = go.transform.FindChild("BuyOne"); 
    t1.GetComponent<Button>().onClick.AddListener(delegate { 
     doBuySkill(sk); 
    }); 
    t1.GetChild(0).GetComponent<Text>().text = Main.AsCurrency(sk.getCost(1)) + " pts"; 
    //track how many... 
    i++; 
} 
//update rect transform 
((RectTransform)skillListParent).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, (i * 110 + 10));