TurnOnOffTimePeriod.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. [DefaultExecutionOrder(85)]
  5. public class TurnOnOffTimePeriod : MonoBehaviour
  6. {
  7. // to set the interactivity
  8. [Header("Toggle Interactivity:")]
  9. public bool interactable = true;
  10. public GameObject legend;
  11. // compare toggle with the visibility
  12. private bool year2019 = false, year2020 = false, year2021 = false;
  13. // Access the humans body to set the visibility
  14. //private List<Component> humansBody2019 = new List<Component>();
  15. //private List<Component> humansBody2020 = new List<Component>();
  16. //private List<Component> humansBody2021 = new List<Component>();
  17. private GameObject[][] humansBody2019;
  18. private GameObject[][] humansBody2020;
  19. private GameObject[][] humansBody2021;
  20. // Toggle GameObject
  21. private Toggle[] toggleGO;
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. Component[] years = GetComponentsInChildren<InstantiatePrefab>(true);
  26. foreach (var year in years)
  27. {
  28. GameObject[][] humansGO = year.GetComponent<InstantiatePrefab>().humanGameObject;
  29. if (humansGO[0][0].CompareTag("2019"))
  30. humansBody2019 = humansGO;
  31. else if (humansGO[0][0].CompareTag("2020"))
  32. humansBody2020 = humansGO;
  33. else if (humansGO[0][0].CompareTag("2021"))
  34. humansBody2021 = humansGO;
  35. }
  36. toggleGO = legend.GetComponentsInChildren<Toggle>();
  37. if (!interactable)
  38. foreach(Toggle toggle in toggleGO)
  39. toggle.interactable = false;
  40. else
  41. {
  42. string toggleName = "Visibility 2019";
  43. foreach (var toggle in toggleGO)
  44. if (toggle.name != toggleName)
  45. toggle.isOn = false;
  46. }
  47. }
  48. public void OnVisibility2019Press()
  49. {
  50. if (year2019 == true)
  51. {
  52. changeVisibility(humansBody2019, true);
  53. year2019 = false;
  54. if (!year2020) toggleGO[1].isOn = false;
  55. if (!year2021) toggleGO[2].isOn = false;
  56. }
  57. else
  58. {
  59. changeVisibility(humansBody2019, false);
  60. year2019 = true;
  61. }
  62. }
  63. public void OnVisibility2020Press()
  64. {
  65. if (year2020 == true)
  66. {
  67. changeVisibility(humansBody2020, true);
  68. year2020 = false;
  69. if (!year2019) toggleGO[0].isOn = false;
  70. if (!year2021) toggleGO[2].isOn = false;
  71. }
  72. else
  73. {
  74. changeVisibility(humansBody2020, false);
  75. year2020 = true;
  76. }
  77. }
  78. public void OnVisibility2021Press()
  79. {
  80. if (year2021 == true)
  81. {
  82. changeVisibility(humansBody2021, true);
  83. year2021 = false;
  84. if (!year2019) toggleGO[0].isOn = false;
  85. if (!year2020) toggleGO[1].isOn = false;
  86. }
  87. else
  88. {
  89. changeVisibility(humansBody2021, false);
  90. year2021 = true;
  91. }
  92. }
  93. private void changeVisibility(GameObject[][] humansBodies, bool activeStatus)
  94. {
  95. for(int i = 0; i < humansBodies.Length; ++i)
  96. {
  97. for(int j = 0; j < humansBodies[i].Length; ++j)
  98. {
  99. humansBodies[i][j].gameObject.SetActive(activeStatus);
  100. }
  101. }
  102. }
  103. }