Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Add List<string> to Scrollview.Content?

Discussion in 'Scripting' started by mholmes, Jan 15, 2019.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I'm trying to loop through a list and add the values to a scroll view content as a text object. Here is my code but not working currently/not finished. Not sure how to add objects to the scrollview.content.

    Code (CSharp):
    1. public GameObject _Content;
    2.  
    3.     private Text _Text;
    4.     private List<string> lst = new List<string>();
    5.  
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.         //Logged In Redirect
    10.         //if (!User_Data._Logged_In)
    11.         //{
    12.         //    SceneManager.LoadScene("Log_In");
    13.         //}
    14.         //else
    15.         //{
    16.  
    17.         //}
    18.     }
    19.  
    20.     private void Build_Promotional_Codes()
    21.     {
    22.         int index = 0;
    23.  
    24.         lst.Add("Promotional code 1");
    25.         lst.Add("Promotional code 2");
    26.         lst.Add("Promotional code 3");
    27.         lst.Add("Promotional code 4");
    28.         lst.Add("Promotional code 5");
    29.  
    30.         foreach(string str in lst)
    31.         {
    32.             _Text.text = lst.ToString();
    33.             //_Content.
    34.         }
    35.     }
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You need to assign your list strings values to text gameobjects and then parent (use transform.SetParent) those gameobjects to the content object of the scrollview.
     
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Can you provide sample code? Thanks
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    The problem you're having is that you only have one text component(that i assume is already in the scene, and you're getting a reference to) and you're overriding the text that it has, you need to either have a new component for every line (like @Munchy2007 said) and set it's text to what ever, or keep it as one but add the texts instead of overriding it and use new line mod ( /n or System.Enviroment.NewLine)

    The better option is to have a new object for each element you want to display, imo.
    Have a blank-slate object that have all the setting needed(like font, size, color, w/e), have it as a prefab, instantiate it, parent it to the content, set its text.

    I can provide a some-what related sample code when I get back from work.(from one of my games shop UI)
     
  5. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Ooooo hh right right right yes I need to have it create a new instance of the Text object each time. I'm sorry I over looked that detail. Yes a code sample would be great. Thank you very much for your help.
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    a bit late, but here's the shop that fills my UI shop
    Code (CSharp):
    1.         void PopulateShop(){
    2.  
    3.             Part[] parts = Resources.LoadAll ("Parts", typeof(Part)).Cast<Part> ().ToArray (); //get all the elements you want to display
    4.  
    5.             for (int i = 0; i < parts.Length; i++) {
    6.                
    7.                 GameObject go = (GameObject)Instantiate (UIPartShop, shopContent.transform, false); //Spawn a 'blank' object and parent that to the content
    8.                 UI_Part uip = go.GetComponent<UI_Part> (); //Get the reference to the script on the object
    9.                 uip.Setup (parts [i]); //Set it up with the information from parts[i]
    10.                 go.GetComponentInChildren<Button> ().onClick.AddListener (uip.Buy); //Find it's button and add the function "Buy" to it
    11.             }
    12.  
    13.         }
     
    tomasantunes likes this.
  7. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Not sure I follow your logic. Do you have a tut or something I can review? I have tried watching several tuts on this topic and everyone seems to be adding their objects by hand. I think what I need is more of a Data Table or list box. What I will be doing is pulling down a data table from my data base. It will have a list of Promotional Codes the user can use. I'd like to use a button so they can click on it and it auto fills a input field for them but a simple text list would be fine too. I just need something dynamic to build a list.

    In a nutshell I just need to know how to build the object via script or dynamically and add it to the scroll view content. If any one has a tut on how to do this that would be great. Thanks
     
  8. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I think I found the answer to my question. I found this tut: https://www.studica.com/blog/unity-ui-tutorial-scroll-grid

    came up with this logic. Still building it but you get the idea:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class Load_Promotional_Code : MonoBehaviour
    8. {
    9.     public GameObject _txtPromotionalCode;// This is our prefab object that will be exposed in the inspector
    10.     private List<string> _lstPromotionalCodes = new List<string>();
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         User_Data._Current_Scene = "Promotional_Code";
    16.  
    17.         //Logged In Redirect
    18.         //if (!User_Data._Logged_In)
    19.         //{
    20.         //    SceneManager.LoadScene("Log_In");
    21.         //}
    22.         //else
    23.         //{
    24.  
    25.         //}
    26.     }
    27.  
    28.     void Populate()
    29.     {
    30.         GameObject obj; // Create GameObject instance  
    31.         int count = 0; //get this value from datatable count returned from database
    32.  
    33.         for (int i = 0; i < count; i++)
    34.         {
    35.             // Create new instances of our prefab until we've created as many as we specified
    36.             obj = (GameObject)Instantiate(_txtPromotionalCode, transform);
    37.  
    38.             obj.GetComponent<Text>().text = "";
    39.         }
    40.     }
    41. }
    42.