using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class ImageAcquisition : MonoBehaviour { public List lookAtCameras; /// /// 车辆 /// public Toggle Car; /// /// 行人 /// public Toggle PeoPle; /// /// 红绿灯 /// public Toggle Light; private Coroutine coroutine; void Start() { coroutine = StartCoroutine(GetImageType("0")); StopCoroutine(coroutine); Car.onValueChanged.AddListener((a) => { HideImage(); if (Car.isOn) { coroutine = StartCoroutine(GetImageType("车")); } else { StopCoroutine(coroutine); lookAtCameras = new List(); } }); PeoPle.onValueChanged.AddListener((a) => { HideImage(); if (PeoPle.isOn) { coroutine = StartCoroutine(GetImageType("人")); } else { StopCoroutine(coroutine); lookAtCameras = new List(); } }); Light.onValueChanged.AddListener((a) => { HideImage(); if (Light.isOn) { coroutine = StartCoroutine(GetImageType("红绿灯")); } else { StopCoroutine(coroutine); lookAtCameras = new List(); } }); } /// /// 隐藏图片 /// public void HideImage() { if (lookAtCameras.Count > 0) { for (int i = 0; i < lookAtCameras.Count; i++) { if (lookAtCameras[i] == null || lookAtCameras[i].transform.childCount < 1) continue; lookAtCameras[i].GetComponentInChildren().chirenimage.SetActive(false); } } } /// /// 显示图片 /// public void ShowImage() { for (int i = 0; i < lookAtCameras.Count; i++) { if (lookAtCameras[i] == null || lookAtCameras[i].transform.childCount < 1) continue; lookAtCameras[i].GetComponentInChildren().chirenimage.SetActive(true); } } IEnumerator GetImageType(string type) { if (type == "0") yield return null; while (true) { yield return new WaitForSeconds(0.5f); if (type == "车") { lookAtCameras = GameObject.FindGameObjectsWithTag("Car").ToList(); ShowImage(); } else if (type == "人") { lookAtCameras = GameObject.FindGameObjectsWithTag("People").ToList(); ShowImage(); } else if (type == "红绿灯") { lookAtCameras = GameObject.FindGameObjectsWithTag("红绿灯").ToList(); ShowImage(); } } } }