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 humansBody2019 = new List(); //private List humansBody2020 = new List(); //private List humansBody2021 = new List(); 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(true); foreach (var year in years) { GameObject[][] humansGO = year.GetComponent().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(); 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); } } } }