Search Unity

[SOLVED] Trying to create a dynamic scroll view

Discussion in 'UGUI & TextMesh Pro' started by pKallv, Mar 24, 2015.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I am trying to create a dynamic scroll view, from a tutorial, but can't see the content to show when it is instantiated into the scroll list.


    The orange panel is the GameObject that i am trying to display in the list. The position marker is the orange object in the list.


    Here is the Hierarchy, with the selected orange object

    replace.png
    Panel is the orange object.





    Here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class ScrollList_Script : MonoBehaviour {
    8.  
    9.     public GameObject itemPrefab;
    10.     public int itemCount = 10, columnCount = 1;
    11.  
    12.     void Awake () {
    13.  
    14.         RectTransform rowRectTransform = itemPrefab.GetComponent<RectTransform>();
    15.         RectTransform containerRectTransform = gameObject.GetComponent<RectTransform>();
    16.    
    17.         //calculate the width and height of each child item.
    18.         float width = containerRectTransform.rect.width / columnCount;
    19.         float ratio = width / rowRectTransform.rect.width;
    20.         float height = rowRectTransform.rect.height * ratio;
    21.         int rowCount = itemCount / columnCount;
    22.         if (itemCount % rowCount > 0)
    23.             rowCount++;
    24.    
    25.         //adjust the height of the container so that it will just barely fit all its children
    26.         float scrollHeight = height * rowCount;
    27.         containerRectTransform.offsetMin = new Vector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
    28.         containerRectTransform.offsetMax = new Vector2(containerRectTransform.offsetMax.x, scrollHeight / 2);
    29.    
    30.         int j = 0;
    31.         for (int i = 0; i < itemCount; i++)
    32.         {
    33.             //this is used instead of a double for loop because itemCount may not fit perfectly into the rows/columns
    34.             if (i % columnCount == 0)
    35.                 j++;
    36.        
    37.             //create a new item, name it, and set the parent
    38.             GameObject newItem = Instantiate(itemPrefab) as GameObject;
    39.             newItem.name = gameObject.name + " item at (" + i + "," + j + ")";
    40.             newItem.transform.parent = gameObject.transform;
    41.        
    42.             //move and size the new item
    43.             RectTransform rectTransform = newItem.GetComponent<RectTransform>();
    44.        
    45.             float x = -containerRectTransform.rect.width / 2 + width * (i % columnCount);
    46.             float y = containerRectTransform.rect.height / 2 - height * j;
    47.             rectTransform.offsetMin = new Vector2(x, y);
    48.        
    49.             x = rectTransform.offsetMin.x + width;
    50.             y = rectTransform.offsetMin.y + height;
    51.             rectTransform.offsetMax = new Vector2(x, y);
    52.         }
    53.    
    54.    
    55.     }
    56.  
    57.  
    58. }
     
  2. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    PROBLEM SOLVED
    I finally found the problem by changing line 40 to:

    Code (CSharp):
    1. newItem.transform.SetParent(gameObject.transform, false);
    :)
     
    RandomAgent and SimonDarksideJ like this.
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
  4. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    Wouldn't it be a bit easier (=less code)to use layout group functionalities? It seems you used the code from the splendid dynamic list youtube tutorial; I used that myself as well. At some point I noticed I could replace most of the code by using layout groups/elements which made the overall code much cleaner while retaining pretty much the same functionality.

    Edit: Link to documentation
     
    Mycroft likes this.
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    You are correct, that is what i use! Would you happen to have some examples?
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Oh in that case I just completely misunderstood your issue :D

    (I was going to say check the learn tuts, but there isn't one for layout)
    Watch @Adam Buckner 's lists live training video, which has many great tips http://unity3d.com/learn/tutorials/...ing-archive/creating-scroll-lists-at-run-time

    It is strange looking and not finding anything on layouts.

    Put simply though, you just need to add a "Vertical Layout Group" on your pnl_Content GO and it will automatically organise all the child elements within it's RectTransform area.
    By default it will stretch all children to layout uniformly in the area, if you don't want that add LayoutElement components to each of the children and set their preferred height, then disable the "Force Child Expand" option on the group.

    Hope this helps.
    The layout groups are VERY easy to use to lay things out.
    (Don't forget to disable the script above :p)
     
  7. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    great thanks :)
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    SimonDarksideJ likes this.
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  10. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
  11. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Sounds like a dedicated "layout" session is called for @Adam Buckner :D (or a good book :p)
     
  12. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Now @SimonDarksideJ you keep mentioning this "book", are you writing one? Know of anybody who is?
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The book is your problem, Simon!

    If anything, I'll write one on coding for unity.
     
  14. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Well my signature kind of gives that away a bit @Ramcat :D
     
  15. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Definitely need more "Best practices on coding in Unity" books @Adam Buckner, Talk to Will, he'll get you plugged in.
    My first title made a start, tag you're it!
     
  16. Hoskins355

    Hoskins355

    Joined:
    Jan 3, 2013
    Posts:
    142
    Thanks That fixed my problem too
     
    pKallv likes this.
  17. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    Last edited: Aug 27, 2016
    jorgecastillodp likes this.