TurnOnOffTimePeriod.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class TurnOnOffTimePeriod : MonoBehaviour
  4. {
  5. public bool year2019 = false, year2020 = false, year2021 = false;
  6. private List<Component> humansBody2019 = new List<Component>();
  7. private List<Component> humansBody2020 = new List<Component>();
  8. private List<Component> humansBody2021 = new List<Component>();
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. Component[] obj = GetComponentsInChildren<Transform>(true);
  13. foreach (Transform child in obj)
  14. {
  15. if (child.parent == obj[0])
  16. continue;
  17. if (child.CompareTag("2019"))
  18. {
  19. humansBody2019.Add(child);
  20. if(!year2019)
  21. year2019 = child.gameObject.activeSelf;
  22. }
  23. else if (child.CompareTag("2020"))
  24. {
  25. humansBody2020.Add(child);
  26. if(!year2020)
  27. year2020 = child.gameObject.activeSelf;
  28. }
  29. else if(child.CompareTag("2021"))
  30. {
  31. humansBody2021.Add(child);
  32. if(!year2021)
  33. year2021 = child.gameObject.activeSelf;
  34. }
  35. }
  36. }
  37. private void Update()
  38. {
  39. if (year2019 == true)
  40. changeVisibility(humansBody2019, true);
  41. else
  42. changeVisibility(humansBody2019, false);
  43. if (year2020 == true)
  44. changeVisibility(humansBody2020, true);
  45. else
  46. changeVisibility(humansBody2020, false);
  47. if (year2021 == true)
  48. changeVisibility(humansBody2021, true);
  49. else
  50. changeVisibility(humansBody2021, false);
  51. }
  52. private void changeVisibility(List<Component> humansBodies, bool activeStatus)
  53. {
  54. foreach (Transform humanBody in humansBodies)
  55. foreach (Transform child in humanBody)
  56. child.gameObject.SetActive(activeStatus);
  57. }
  58. }