Search Unity

Scroll view list instantiation issues

Discussion in 'UGUI & TextMesh Pro' started by ashley, Mar 2, 2017.

  1. ashley

    ashley

    Joined:
    Nov 5, 2011
    Posts:
    84
    Hi all,

    Trying to create a scroll view which is a list of smaller canvases.

    The basic structure is as follows:

    - Panel area (scroll rect, mask, image)
    --Content panel (vertical layout group, content size fitter with vertical fit set to min size)
    ---Instantiated panel (layout element with preferred height set to 100)
    ----Panel components (text, image etc etc)

    When manually adding them it works fine. However, I want to instantiate them at runtime as the list will be randomly populated with x from y possible panels.

    I've got a class for the panel (Person) and a list of those attached to a GameObject in which I'll build up the possible entries.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6. using System.Collections.Generic;
    7.  
    8. [System.Serializable]
    9. public class Person {
    10.     public string name;
    11.     public Sprite icon;
    12. }
    13.  
    14. public class CreateScrollList : MonoBehaviour {
    15.  
    16.     public GameObject namePrefab;
    17.     public List<Person> personList;
    18.  
    19.     public Transform contentPanel;
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.  
    24.         PopulateList ();
    25.     }
    26.  
    27.     void PopulateList()
    28.     {
    29.         foreach (var person in personList) {
    30.             GameObject newPerson = Instantiate (namePrefab) as GameObject;
    31.             NamePrefab generatedName = newPerson.GetComponent <NamePrefab> ();
    32.             generatedName.nameLabel.text = person.name;
    33.             generatedName.icon = person.icon;
    34.             newPerson.transform.SetParent (contentPanel);
    35.  
    36.         }
    37.     }
    38. }
    39.  
    This adds them to the contentPanel (the 'Content Panel' listed above) and updates the UI elements as requested, but for some reason changes the scale to 2.918919.

    I can't figure out exactly why it's doing that, nor how to manually force the scale back.

    Been following the video below but the scale seems to be an issue.



    I've tried changing the layout element and the content fitter but to no avail.

    Feel free to talk to me like I'm stupid because I am when it comes to UI!

    Fixed

    Someone else informed me to set worldPositionStays to false when setting the parents as described here: https://docs.unity3d.com/ScriptReference/Transform.SetParent.html

    (i.e. newPerson.transform.SetParent (contentPanel, false))

    Leaving in case anyone else could use this in the future (and I can't seem to delete)
     
    Last edited: Mar 2, 2017
    Archviz3d likes this.
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096