daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public RectTransform GridWithNameElements;
  2. public GameObject StudentNamePrefabButton;
  3.  
  4. while (reader.Read())
  5.             {
  6.                 //create a new button object and use the prefab button to make sure spacing etc is correct
  7.                 goButton = (GameObject)Instantiate(StudentNamePrefabButton);
  8.                 //set the parent of the button
  9.                 goButton.transform.SetParent(GridWithNameElements, false);
  10.                 goButton.transform.localScale = new Vector3(1, 1, 1);
  11.                 //set the text of the button. Array value is 0 as the student name is always at position 0 on each iteration
  12.                 goButton.GetComponentsInChildren<Text>()[0].text = reader["fullName"].ToString()+" | "+ reader["studentNumber"].ToString();
  13.                 goButton.name = reader["studentID"].ToString();
  14.  
  15.                 Button tempButton = goButton.GetComponent<Button>();
  16.                 int tempInt = i;
  17.  
  18.                 tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
  19.  
  20.                 i++;
  21.  
  22.                 Debug.Log(goButton.name);
  23.             }
  24.    
  25. //This method is called when the student attempts to search for their own name when taking the individual quiz
  26.     public void SearchExistingStudentName()
  27.     {
  28.  
  29.         //Get the name entered from the text field
  30.         string student_name = searchExistingStudentNameInput.text;
  31.  
  32.         //Add names that exist to the new list
  33.         List<string> names = new List<string>();
  34.         names.Clear();
  35.         //count for onclick listener for new button(s)
  36.         int x = 0;
  37.  
  38.             //loop through the list of buttons with student names
  39.             for (int i = 0; i < GridWithNameElements.childCount; i++)
  40.             {
  41.  
  42.             //Check if the user has typed their name with capitals or lowercase etc, and check the inputted value against names already in the list
  43.             if (GridWithNameElements
  44.                .GetComponentsInChildren<Text>()[i].text.Contains(student_name) || GridWithNameElements
  45.                .GetComponentsInChildren<Text>()[i].text.ToLower().Contains(student_name) || GridWithNameElements
  46.                .GetComponentsInChildren<Text>()[i].text.ToUpper().Contains(student_name))
  47.                 {
  48.                     //If the name entered contains letters found in the parent GridWithNameElements, then add them to the list array and their name value (unique id)
  49.                     names.Add(GridWithNameElements.GetComponentsInChildren<Text>()[i].text.Replace(@"[", string.Empty).Replace(@"]", string.Empty));
  50.                     names.Add(GridWithNameElements.GetChild(i).name); //this is the unique id of the student
  51.  
  52.                     //Loop through and hide all children and hide them
  53.                     foreach (Transform child in GridWithNameElements)
  54.                     {
  55.                         child.gameObject.SetActive(false);
  56.                     }
  57.  
  58.                     //Then create a button that represents a name added to the names List
  59.                     newButton = (GameObject)Instantiate(StudentNamePrefabButton);
  60.                     //set the parent of the button
  61.                     newButton.transform.SetParent(GridWithNameElements, false);
  62.                     newButton.transform.localScale = new Vector3(1, 1, 1);
  63.                     //set the text of the button. Array value is 0 as the student name is always at position 0 on each iteration
  64.                     newButton.GetComponentsInChildren<Text>()[0].text = names[0].ToString();
  65.                     newButton.name = names[1].ToString().Trim();
  66.  
  67.                     //Then add a click listener to the button
  68.                     Button tempButton = newButton.GetComponent<Button>();
  69.                     int tempInt = x;
  70.  
  71.                     tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
  72.  
  73.                     x++;
  74.  
  75.                     Debug.Log("Student Unique ID " + newButton.name);
  76.  
  77.                 }
  78.                 //if a new button object exists and the name was entered and then removed, clear it from the list and remove the created button
  79.                 else if (GameObject.Find(newButton.name) != null && student_name == "")
  80.                 {
  81.  
  82.                     names.Clear();
  83.                     Destroy(newButton.gameObject);
  84.  
  85.                     //Loop through and show all children
  86.                     foreach (Transform child in GridWithNameElements)
  87.                     {
  88.                         child.gameObject.SetActive(true);
  89.                     }
  90.  
  91.                 }
  92.             }
  93.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top