using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
///
/// 控制红绿灯
///
public class ControlLight : MonoBehaviour
{
public static ControlLight Instance;
///
/// 1号绿灯Toggle
///
public Toggle Green1;
///
/// 1号红灯toggle
///
public Toggle Red1;
///
/// 1号确认按钮
///
public Button Enter1;
///
/// 1号输入的倒计时
///
public TMP_InputField WaitTime1;
///
/// 1号语音按钮
///
public Button AudiosBt1;
///
/// 2号绿灯Toggle
///
public Toggle Green2;
///
/// 2号红灯toggle
///
public Toggle Red2;
///
/// 2号确认按钮
///
public Button Enter2;
///
/// 2号输入的倒计时
///
public TMP_InputField WaitTime2;
///
/// 2号语音按钮
///
public Button AudiosBt2;
///
/// 被点击的红绿灯模型
///
public GameObject lightobj1;
///
/// 红灯
///
public GameObject redlight1;
///
/// 黄灯
///
public GameObject yellowlight1;
///
/// 绿灯
///
public GameObject greenlight1;
///
/// 红绿灯时间
///
public TextMeshPro LightNumber1;
///
/// 1号红绿灯空气墙
///
public GameObject LightBox1;
///
/// 被点击的红绿灯模型
///
public GameObject lightobj2;
///
/// 红灯
///
public GameObject redlight2;
///
/// 黄灯
///
public GameObject yellowlight2;
///
/// 绿灯
///
public GameObject greenlight2;
///
/// 红绿灯时间
///
public TextMeshPro LightNumber2;
///
/// 2号红绿灯空气墙
///
public GameObject LightBox2;
///
/// 计数:1号语音
///
int index1 = 0;
///
/// 计数:2号语音
///
int index2 = 0;
private void Awake()
{
Instance = this;
}
void Start()
{
AudiosBt1.onClick.AddListener(() =>
{
index1++;
if (index1 == 1)
{
Enter1.interactable = false;
AudiosBt2.interactable = false;
}
else if (index1 == 2)
{
index1 = 0;
AudiosBt1.interactable = false;
AudiosBt2.interactable = true;
}
});
AudiosBt2.onClick.AddListener(() =>
{
index2++;
if (index2 == 1)
{
Enter2.interactable = false;
AudiosBt1.interactable = false;
}
else if (index2 == 2)
{
index2 = 0;
AudiosBt1.interactable = true;
AudiosBt2.interactable = false;
}
});
Enter1.onClick.AddListener(() =>
{
AudiosBt1.interactable = false;
Enter1.interactable = false;
LightNumber1.text = WaitTime1.text;
if (Green1.isOn)
{
Red1.interactable = false;
StartCoroutine(TimeSub_1(int.Parse(LightNumber1.text), "红"));
StartCoroutine(GreenToRed_1(int.Parse(LightNumber1.text), greenlight1, redlight1));
}
else if (Red1.isOn)
{
Green1.interactable = false;
StartCoroutine(TimeSub_1(int.Parse(LightNumber1.text), "绿"));
StartCoroutine(RedToGreen_1(int.Parse(LightNumber1.text), greenlight1, redlight1));
}
});
Enter2.onClick.AddListener(() =>
{
AudiosBt2.interactable = false;
Enter2.interactable = false;
LightNumber2.text = WaitTime2.text;
if (Green2.isOn)
{
Red2.interactable = false;
StartCoroutine(TimeSub_2(int.Parse(LightNumber2.text), "红"));
StartCoroutine(GreenToRed_2(int.Parse(LightNumber2.text), greenlight2, redlight2));
}
else if (Red2.isOn)
{
Green2.interactable = false;
StartCoroutine(TimeSub_2(int.Parse(LightNumber2.text), "绿"));
StartCoroutine(RedToGreen_2(int.Parse(LightNumber2.text), greenlight2, redlight2));
}
});
}
private IEnumerator GreenToRed_1(int waittiem, GameObject Green, GameObject red)
{
yield return new WaitForSeconds(waittiem);
Green.GetComponent().material.DisableKeyword("_EMISSION");
yellowlight1.GetComponent().material.EnableKeyword("_EMISSION");
LightNumber1.color = Color.green;
Green.GetComponent().material.DisableKeyword("_EMISSION");
yield return new WaitForSeconds(1);
red.GetComponent().material.EnableKeyword("_EMISSION");
yellowlight1.GetComponent().material.DisableKeyword("_EMISSION");
LightNumber1.color = Color.red;
LightNumber1.text = "99";
}
private IEnumerator RedToGreen_1(int waittiem, GameObject Green, GameObject red)
{
yield return new WaitForSeconds(waittiem);
red.GetComponent().material.DisableKeyword("_EMISSION");
LightNumber1.color = Color.red;
yellowlight1.GetComponent().material.EnableKeyword("_EMISSION");
red.GetComponent().material.DisableKeyword("_EMISSION");
yield return new WaitForSeconds(1);
yellowlight1.GetComponent().material.DisableKeyword("_EMISSION");
Green.GetComponent().material.EnableKeyword("_EMISSION");
LightNumber1.color = Color.green;
LightNumber1.text = "99";
}
///
/// 倒计时
///
private IEnumerator TimeSub_1(int waittiem, string type)
{
for (int i = waittiem; i >= 0; i--)
{
LightNumber1.text = i.ToString();
yield return new WaitForSeconds(1);
}
if (type == "绿")
{
Red1.isOn = false;
Green1.isOn = true;
Red1.interactable = false;
Green1.interactable = true;
Enter1.interactable = true;
AudiosBt1.interactable = true;
LightBox1.SetActive(false);
}
else
{
Red1.isOn = true;
Green1.isOn = false;
Red1.interactable = true;
Green1.interactable = false;
Enter1.interactable = true;
AudiosBt1.interactable = true;
LightBox1.SetActive(true);
}
}
private IEnumerator GreenToRed_2(int waittiem, GameObject Green, GameObject red)
{
yield return new WaitForSeconds(waittiem);
Green.GetComponent().material.DisableKeyword("_EMISSION");
yellowlight2.GetComponent().material.EnableKeyword("_EMISSION");
LightNumber2.color = Color.green;
Green.GetComponent().material.DisableKeyword("_EMISSION");
yield return new WaitForSeconds(1);
red.GetComponent().material.EnableKeyword("_EMISSION");
yellowlight2.GetComponent().material.DisableKeyword("_EMISSION");
LightNumber2.color = Color.red;
LightNumber2.text = "99";
}
private IEnumerator RedToGreen_2(int waittiem, GameObject Green, GameObject red)
{
yield return new WaitForSeconds(waittiem);
red.GetComponent().material.DisableKeyword("_EMISSION");
LightNumber2.color = Color.red;
yellowlight2.GetComponent().material.EnableKeyword("_EMISSION");
red.GetComponent().material.DisableKeyword("_EMISSION");
yield return new WaitForSeconds(1);
yellowlight2.GetComponent().material.DisableKeyword("_EMISSION");
Green.GetComponent().material.EnableKeyword("_EMISSION");
LightNumber2.color = Color.green;
LightNumber2.text = "99";
}
///
/// 倒计时
///
private IEnumerator TimeSub_2(int waittiem, string type)
{
for (int i = waittiem; i >= 0; i--)
{
LightNumber2.text = i.ToString();
yield return new WaitForSeconds(1);
}
if (type == "绿")
{
Red2.isOn = false;
Green2.isOn = true;
Red2.interactable = false;
Green2.interactable = true;
Enter2.interactable = true;
AudiosBt2.interactable = true;
LightBox2.SetActive(false);
}
else
{
Red2.isOn = true;
Green2.isOn = false;
Red2.interactable = true;
Green2.interactable = false;
Enter2.interactable = true;
AudiosBt2.interactable = true;
LightBox2.SetActive(true);
}
}
///
/// 语音控制红绿灯
///
/// 哪一个红绿灯
/// 说话内容
public void AudiosTalk(string types, string talkstr)
{
if (types == "1号红绿灯")
{
if (talkstr.Contains("红灯"))
{
if (Green1.interactable)
{
StartCoroutine(ImageTips.instance.WaitHide());
ImageTips.instance.Tips.text = "当前不支持切换为红灯,请先设置绿灯时间后再设置。";
}
else
{
Red1.interactable = false;
StartCoroutine(TimeSub_1(20, "绿"));
StartCoroutine(RedToGreen_1(20, greenlight1, redlight1));
}
}
else if (talkstr.Contains("绿灯"))
{
if (Red1.interactable)
{
StartCoroutine(ImageTips.instance.WaitHide());
ImageTips.instance.Tips.text = "当前不支持切换为绿灯,请先设置红灯时间后再设置。";
}
else
{
Enter1.interactable = false;
Green1.interactable = false;
StartCoroutine(TimeSub_1(20, "红"));
StartCoroutine(GreenToRed_1(20, greenlight1, redlight1));
}
}
else if (!talkstr.Contains("红灯") && !talkstr.Contains("绿灯"))
{
Enter1.interactable = true;
AudiosBt1.interactable = true;
AudiosBt2.interactable = true;
}
}
else if (types == "2号红绿灯")
{
if (talkstr.Contains("红灯"))
{
if (Green2.interactable)
{
StartCoroutine(ImageTips.instance.WaitHide());
ImageTips.instance.Tips.text = "当前不支持切换为红灯,请先设置绿灯时间后再设置。";
}
else
{
Red2.interactable = false;
StartCoroutine(TimeSub_2(20, "绿"));
StartCoroutine(RedToGreen_2(20, greenlight2, redlight2));
}
}
else if (talkstr.Contains("绿灯"))
{
if (Red2.interactable)
{
StartCoroutine(ImageTips.instance.WaitHide());
ImageTips.instance.Tips.text = "当前不支持切换为绿灯,请先设置红灯时间后再设置。";
}
else
{
Enter2.interactable = false;
Green2.interactable = false;
StartCoroutine(TimeSub_2(20, "红"));
StartCoroutine(GreenToRed_2(20, greenlight2, redlight2));
}
}
else if (!talkstr.Contains("红灯") && !talkstr.Contains("绿灯"))
{
Enter2.interactable = true;
AudiosBt1.interactable = true;
AudiosBt2.interactable = true;
}
Debug.Log(!talkstr.Contains("红灯")+","+ !talkstr.Contains("绿灯"));
}
}
}