public void SetFriends(OKUserInfo[] friends) { // Store all friend objects foreach (OKUserInfo friend in friends) { Friend f = new Friend(friend.uid, friend.name, friend.pic128x128, defaultPhoto); friendObjectList.Add(f); } // Let's scale the friends container manually! (Explained below, why) RectTransform friendsContainerRect = friendsContainer.GetComponent(typeof(RectTransform)) as RectTransform; friendsContainerRect.sizeDelta = new Vector2(friendObjectList.Count * friendGap, friendsContainerRect.sizeDelta.y); int optimalObjectCount = (int)((Screen.width / friendGap) + 8); childCount = Math.Min(optimalObjectCount, friendObjectList.Count); // Fill initial children. int x = 0; for (int i = 0; i < childCount; i++) { TestFriendSlot newFriendSlot = (TestFriendSlot)Instantiate(friendPrefab); newFriendSlot.transform.SetParent(friendsContainer.transform, false); newFriendSlot.transform.localPosition = new Vector3(x * friendGap, newFriendSlot.transform.localPosition.y, 0); friendPrefabList.Add(newFriendSlot); newFriendSlot.SetFriend(friendObjectList[i]); x++; } if (this.friendObjectList.Count == 0) { NoFriends(); } else { noFriendsLabel.gameObject.SetActive(false); } UpdateScrollbar(); ScreenManager.AddOnScreenChanged(UpdateScrollbar); }
public void SetFriends(OKUserInfo[] friends) { // Let's scale the friends container manually! (Explained below, why) RectTransform friendsContainerRect = friendsContainer.GetComponent(typeof(RectTransform)) as RectTransform; friendsContainerRect.sizeDelta = new Vector2(friends.Length * friendGap, friendsContainerRect.sizeDelta.y); int x = 0; foreach (OKUserInfo friend in friends) { Friend f = new Friend(friend.uid, friend.name, friend.pic128x128, defaultPhoto); TestFriendSlot newFriendSlot = (TestFriendSlot)Instantiate(friendPrefab); newFriendSlot.transform.SetParent(friendsContainer.transform, false); // Offset the friend slot - We could the "Horizontal layer group" component, // but it messes with the child objects too much, so that's why we scaled the friends container before! Vector3 newFriendSlotPos = newFriendSlot.transform.localPosition; newFriendSlot.transform.localPosition = new Vector3(x * friendGap, newFriendSlotPos.y, 0); // Give the slot its friend data newFriendSlot.SetFriend(f); this.friends.Add(f); x++; } if (this.friends.Count == 0) { NoFriends(); } else { noFriendsLabel.gameObject.SetActive(false); } UpdateScrollbar(); ScreenManager.AddOnScreenChanged(UpdateScrollbar); }
private void UpdatePositions(int position) { if (position != currentPositionX) { currentPositionX = position; int processedItems = 0; for (int i = position - 5; i < position + childCount; i++) { // End when all prefabs had their position set once, starting from left side. if (processedItems >= childCount) { break; } if (i >= 0 && i < friendObjectList.Count) { TestFriendSlot item = friendPrefabList[i % childCount]; // Each item has a specific position already reserved for it during initialization. item.transform.localPosition = new Vector3(i * friendGap, item.transform.localPosition.y, 0); item.SetFriend(friendObjectList[i]); processedItems++; } } } }