• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Martin_Degabriele · Jun 24 at 04:50 PM · parentbuttonsscroll view

Creating a dynamic second scroll list, controlled on the selection of the first dynamic scroll list.

Book Controller Script

using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; using System;

public class BookController : MonoBehaviour { public string filepath = "DataWriteRead.txt";

 public int LC = 0; // is the number of lines in the text file
 private int longestCategory = 0;
 private string printCategoryTextButton = "";
 private string printItemTextButton = "";
 private int i;
 
 // declarations of Arrays to store Items Data
 private string[] ItemID = new string[1000];
 private string[] ItemNo = new string[1000];
 private string[] ItemName = new string[1000];
 private string[] SelectedItemsName = new string[1000];
 private int differentNames = 0; // is the number of different Categories
 public string[] ItemCategory = new string[1000];
 private int differentCategories = 0; // is the number of different Categories
 private string[] ItemDescription = new string[1000];
 private string[] ItemCalories = new string[1000];
 private string[] ItemGrms = new string[1000];
 private string[] ItemPictureFilePath = new string[1000];
 private string[] uniqueCategory = new string[1000];
 private string[] sortedCategory = new string[1000];
 private string[] sortedNames = new string[1000];
 private int w = 0;
 public string MyTextString = "Martin";
 private ItemController itemController;
 private List<GameObject> buttons;

 [SerializeField] private GameObject buttonTemplate;     //for category buttons
 [SerializeField] private GameObject buttonTemplate2;     //for category buttons


 void Start()
 {
     //Read text file and prepare a table in array format
     ReadFile();

     uniqueCategory = ItemCategory.Distinct().ToArray(); // create an array with unique categories
     
     string[] sortCategory = new string[uniqueCategory.Length - 1]; // create an array that it will be used as the final button list
     string[] sortAlphabeticalOrderC = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " ", "@", "%", "$", "€", "-", "+", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
     string[] sortedCategory = new string[uniqueCategory.Length];
     int sortedArrayCategoryNo = 0;
     int alphabetLength = sortAlphabeticalOrderC.Length;
     for (int z = 0; z < alphabetLength; z++)
     {
         for (int t = 0; t < sortCategory.Length; t++)
         {
             if (uniqueCategory[t].Substring(0, 1) == sortAlphabeticalOrderC[z])
             {
                 sortedCategory[sortedArrayCategoryNo] = uniqueCategory[t];
                 sortedArrayCategoryNo++;
             }
         }
     }


     // Sort and create the Category Buttons

     for (int t = 0; t < uniqueCategory.Length - 1; t++) //funcion to generate the category Buttons
          {
        
         GameObject button = Instantiate(buttonTemplate) as GameObject; // Create a new button
         button.SetActive(true); // Set the buttons active, false will disable button
         printCategoryTextButton = sortedCategory[t];
         if (printCategoryTextButton is null)
         {
             // do nothing
         }
         else
         {
             button.GetComponent<CategoryButtons>().SetText(printCategoryTextButton); // Set the text of the new button to "Button #" + No
             button.transform.SetParent(buttonTemplate.transform.parent, false); // Set the parent of the new button, as the parent of the button content
             button.GetComponent<CategoryButtons>().myTextString = sortedCategory[t];
         }
        
     } // end of Sort and create the Category Buttons
 }

 

 public void ButtonClicked(string myTextString)
 {
     Debug.Log(myTextString); 
     
     int k = 0;
     for (int g = 0; g < LC; g++) //LC is the number of lines in the text file
     {
         if (ItemCategory[g] == myTextString) //the value of this field is copied from BookController script
         {
             SelectedItemsName[k] = ItemName[g];
             k++;
         }
     }

     int sortedArrayNamesNo = 0;
     int betaLength = 0;
     int beta = 0;
     int h = 0;

     string[] uniqueNames = SelectedItemsName.Distinct().ToArray(); // create an array with unique items Names
     string[] sortNames = new string[uniqueNames.Length - 1]; // create an array that it will be used as the final Name's button list
     string[] SortAlphabeticalOrderN = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " ", "@", "%", "$", "€", "-", "+", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
     string[] sortedNames = new string[uniqueNames.Length];
     sortedArrayNamesNo = 0;
     betaLength = SortAlphabeticalOrderN.Length;
     for (beta = 0; beta < betaLength; beta++)
     {
         for (h = 0; h < sortNames.Length; h++)
         {
             if (uniqueNames[h].Substring(0, 1) == SortAlphabeticalOrderN[beta])
             {
                 sortNames[sortedArrayNamesNo] = uniqueNames[h];
                 sortedArrayNamesNo++;
             }
         }
     }
     
     //funcion to generate the Item Names Buttons 

     // to clear the buttons already created before creating new once.
     
         buttons = new List<GameObject>();
         Debug.Log(buttons.Count);
         if (buttons.Count > 0)
         {
             foreach (GameObject button in buttons)
             {
                 Destroy(button.gameObject);
             }

             buttons.Clear();
         }

         for (int y = 0; y < uniqueNames.Length - 1; y++)
         {
             GameObject button = Instantiate(buttonTemplate2) as GameObject; // Create a new button
             button.SetActive(true); // Set the buttons active, false will disable button
             printItemTextButton = sortedNames[y];
             if (printItemTextButton is null)
             {
                 // do nothing
             }
             else
             {
                 button.GetComponent<ItemButtons>().SetText2(printItemTextButton); // Set the text of the new button to "Button #" + No
                 button.transform.SetParent(buttonTemplate2.transform.parent, false); // Set the parent of the new button, as the parent of the button content
                 button.GetComponent<ItemButtons>().myTextString2 = sortedNames[y];
             }
         }

     Debug.Log(buttons.Count);

 }

 public void ReadFile()
 {
     // set the file path to create array. Read all file until end   
     using (StreamReader reader = new StreamReader(filepath))
     {
         while (!reader.EndOfStream) // keep reading until you reach the end of file
         {
             string LineData = reader.ReadLine();
             string[] Words = LineData.Split("\t");
             for (i = 0; i < LineData.Length; i++)
                 ItemID[LC] = Words[0];
                 ItemNo[LC] = Words[1];
                 ItemName[LC] = Words[2];
                 ItemCategory[LC] = Words[3];
                 ItemDescription[LC] = Words[4];
                 ItemCalories[LC] = Words[5];
                 ItemGrms[LC] = Words[6];
                 ItemPictureFilePath[LC] = Words[7];
                 LC = LC + 1;
             // i=0, until i < Linedta.length, ie. line in not empty it will get a value more than 0, i++ will proceed, if Line is empty i++ is false and stops
             // LineData is the contect of a whole line
             // the commond Split is used to find the content of each cell
             // "\t" referres to TAB, which is being used as a seperator between fields
             // LC is the number of lines in the text file
             // There are 8 fields in each Line,as given above
         }
     }
 }
 

}

Category Buttons (1st Scroll list)

using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro;

public class CategoryButtons : MonoBehaviour

{ [SerializeField] private TextMeshProUGUI myText; //read Text on Button, the variant type should read TextMeshProUGUI and to include "using TMPro" at start;

 [SerializeField]
 private BookController bookController;

 public string test;
 public string myTextString;
 public Button button;
 
 public void SetText(string textString)  //the textString is the text to be set on the button 
 {
     myTextString = textString;
     myText.text = textString;
     
 }
    
 public void OnClick()
 {
   bookController.ButtonClicked(myTextString);
 }

}

Item Buttons (2nd scroll list buttons

using UnityEngine; using UnityEngine.UI; using TMPro;

public class ItemButtons : MonoBehaviour { [SerializeField] private TextMeshProUGUI myText2; //read Text on Button, the variant type should read TextMeshProUGUI and to include "using TMPro" at start;

 public string myTextString2;
 public void SetText2(string textString2)  //the textString is the text to be set on the button 
 {
     myText2.text = textString2;
 }

   

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Martin_Degabriele · Jun 23 at 08:29 PM 0
Share

I have two issues please. (1) the second scroll list buttons are not keeping the parent as their template position. (2) the buttons are not being destroyed in the second scroll list, instead they keep adding.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

uitoolkit slide out menu? (made for mobile but taking all ideas, except for OnGUI calls) 1 Answer

How to parent these multiple objects to othe multiple objects? 2 Answers

Spawn inside of a parent 1 Answer

If A collide with B A will become Child of B - C# 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges