CQ_Intelligent-Technology-T.../Assets/Scripts/智慧交通/ImageAcquisition.cs

118 lines
3.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class ImageAcquisition : MonoBehaviour
{
public List<GameObject> lookAtCameras;
/// <summary>
/// 车辆
/// </summary>
public Toggle Car;
/// <summary>
/// 行人
/// </summary>
public Toggle PeoPle;
/// <summary>
/// 红绿灯
/// </summary>
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<GameObject>();
}
});
PeoPle.onValueChanged.AddListener((a) =>
{
HideImage();
if (PeoPle.isOn)
{
coroutine = StartCoroutine(GetImageType("人"));
}
else
{
StopCoroutine(coroutine);
lookAtCameras = new List<GameObject>();
}
});
Light.onValueChanged.AddListener((a) =>
{
HideImage();
if (Light.isOn)
{
coroutine = StartCoroutine(GetImageType("红绿灯"));
}
else
{
StopCoroutine(coroutine);
lookAtCameras = new List<GameObject>();
}
});
}
/// <summary>
/// 隐藏图片
/// </summary>
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<LookAtCamera>().chirenimage.SetActive(false);
}
}
}
/// <summary>
/// 显示图片
/// </summary>
public void ShowImage()
{
for (int i = 0; i < lookAtCameras.Count; i++)
{
if (lookAtCameras[i] == null || lookAtCameras[i].transform.childCount < 1) continue;
lookAtCameras[i].GetComponentInChildren<LookAtCamera>().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();
}
}
}
}