Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public RectTransform GridWithNameElements;
- public GameObject StudentNamePrefabButton;
- while (reader.Read())
- {
- //create a new button object and use the prefab button to make sure spacing etc is correct
- goButton = (GameObject)Instantiate(StudentNamePrefabButton);
- //set the parent of the button
- goButton.transform.SetParent(GridWithNameElements, false);
- goButton.transform.localScale = new Vector3(1, 1, 1);
- //set the text of the button. Array value is 0 as the student name is always at position 0 on each iteration
- goButton.GetComponentsInChildren<Text>()[0].text = reader["fullName"].ToString()+" | "+ reader["studentNumber"].ToString();
- goButton.name = reader["studentID"].ToString();
- Button tempButton = goButton.GetComponent<Button>();
- int tempInt = i;
- tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
- i++;
- Debug.Log(goButton.name);
- }
- //This method is called when the student attempts to search for their own name when taking the individual quiz
- public void SearchExistingStudentName()
- {
- //Get the name entered from the text field
- string student_name = searchExistingStudentNameInput.text;
- //Add names that exist to the new list
- List<string> names = new List<string>();
- names.Clear();
- //count for onclick listener for new button(s)
- int x = 0;
- //loop through the list of buttons with student names
- for (int i = 0; i < GridWithNameElements.childCount; i++)
- {
- //Check if the user has typed their name with capitals or lowercase etc, and check the inputted value against names already in the list
- if (GridWithNameElements
- .GetComponentsInChildren<Text>()[i].text.Contains(student_name) || GridWithNameElements
- .GetComponentsInChildren<Text>()[i].text.ToLower().Contains(student_name) || GridWithNameElements
- .GetComponentsInChildren<Text>()[i].text.ToUpper().Contains(student_name))
- {
- //If the name entered contains letters found in the parent GridWithNameElements, then add them to the list array and their name value (unique id)
- names.Add(GridWithNameElements.GetComponentsInChildren<Text>()[i].text.Replace(@"[", string.Empty).Replace(@"]", string.Empty));
- names.Add(GridWithNameElements.GetChild(i).name); //this is the unique id of the student
- //Loop through and hide all children and hide them
- foreach (Transform child in GridWithNameElements)
- {
- child.gameObject.SetActive(false);
- }
- //Then create a button that represents a name added to the names List
- newButton = (GameObject)Instantiate(StudentNamePrefabButton);
- //set the parent of the button
- newButton.transform.SetParent(GridWithNameElements, false);
- newButton.transform.localScale = new Vector3(1, 1, 1);
- //set the text of the button. Array value is 0 as the student name is always at position 0 on each iteration
- newButton.GetComponentsInChildren<Text>()[0].text = names[0].ToString();
- newButton.name = names[1].ToString().Trim();
- //Then add a click listener to the button
- Button tempButton = newButton.GetComponent<Button>();
- int tempInt = x;
- tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
- x++;
- Debug.Log("Student Unique ID " + newButton.name);
- }
- //if a new button object exists and the name was entered and then removed, clear it from the list and remove the created button
- else if (GameObject.Find(newButton.name) != null && student_name == "")
- {
- names.Clear();
- Destroy(newButton.gameObject);
- //Loop through and show all children
- foreach (Transform child in GridWithNameElements)
- {
- child.gameObject.SetActive(true);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment