123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- [DefaultExecutionOrder(85)]
- public class TurnOnOffTimePeriod : MonoBehaviour
- {
- // to set the interactivity
- [Header("Toggle Interactivity:")]
- public bool interactable = true;
- public GameObject legend;
- // compare toggle with the visibility
- private bool year2019 = false, year2020 = false, year2021 = false;
- // Access the humans body to set the visibility
- //private List<Component> humansBody2019 = new List<Component>();
- //private List<Component> humansBody2020 = new List<Component>();
- //private List<Component> humansBody2021 = new List<Component>();
- private GameObject[][] humansBody2019;
- private GameObject[][] humansBody2020;
- private GameObject[][] humansBody2021;
- // Toggle GameObject
- private Toggle[] toggleGO;
- // Start is called before the first frame update
- void Start()
- {
- Component[] years = GetComponentsInChildren<InstantiatePrefab>(true);
- foreach (var year in years)
- {
- GameObject[][] humansGO = year.GetComponent<InstantiatePrefab>().humanGameObject;
- if (humansGO[0][0].CompareTag("2019"))
- humansBody2019 = humansGO;
- else if (humansGO[0][0].CompareTag("2020"))
- humansBody2020 = humansGO;
- else if (humansGO[0][0].CompareTag("2021"))
- humansBody2021 = humansGO;
- }
- toggleGO = legend.GetComponentsInChildren<Toggle>();
- if (!interactable)
- foreach(Toggle toggle in toggleGO)
- toggle.interactable = false;
- else
- {
- string toggleName = "Visibility 2019";
- foreach (var toggle in toggleGO)
- if (toggle.name != toggleName)
- toggle.isOn = false;
- }
- }
- public void OnVisibility2019Press()
- {
- if (year2019 == true)
- {
- changeVisibility(humansBody2019, true);
- year2019 = false;
- if (!year2020) toggleGO[1].isOn = false;
- if (!year2021) toggleGO[2].isOn = false;
- }
- else
- {
- changeVisibility(humansBody2019, false);
- year2019 = true;
- }
- }
- public void OnVisibility2020Press()
- {
- if (year2020 == true)
- {
- changeVisibility(humansBody2020, true);
- year2020 = false;
- if (!year2019) toggleGO[0].isOn = false;
- if (!year2021) toggleGO[2].isOn = false;
- }
- else
- {
- changeVisibility(humansBody2020, false);
- year2020 = true;
- }
- }
- public void OnVisibility2021Press()
- {
- if (year2021 == true)
- {
- changeVisibility(humansBody2021, true);
- year2021 = false;
- if (!year2019) toggleGO[0].isOn = false;
- if (!year2020) toggleGO[1].isOn = false;
- }
- else
- {
- changeVisibility(humansBody2021, false);
- year2021 = true;
- }
- }
- private void changeVisibility(GameObject[][] humansBodies, bool activeStatus)
- {
- for(int i = 0; i < humansBodies.Length; ++i)
- {
- for(int j = 0; j < humansBodies[i].Length; ++j)
- {
- humansBodies[i][j].gameObject.SetActive(activeStatus);
- }
- }
- }
- }
|