using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using TMPro; using UnityEngine; public enum TrafficLightState { Red, Yellow, Green } public class TrafficLightManager : MonoBehaviour { [System.Serializable] public class TrafficLightGroup { public List lights = new List(); public bool startsWithGreen; // 标记组是从绿灯开始还是红灯开始 public float timer; // 计时器 public TrafficLightState currentState; public TrafficLightState previousState; // 新增字段,用于保存黄灯之前的状态 public List trafficLights = new List(); public List roadEventTrigger = new List(); public void Initialize() { // 绿灯开始则设为10秒,否则设为39秒 currentState = startsWithGreen ? TrafficLightState.Green : TrafficLightState.Red; previousState = currentState; // 初始化时,之前的状态与当前状态相同 timer = startsWithGreen ? 6f : 16; } public void UpdateTrafficLightTexture(TrafficLightState state) { // Debug.Log("123"); foreach (var light in trafficLights) { if (light != null) { //Texture newTexture = GetTextureForState(state); light.transform.Find("red2").GetComponent().material.DisableKeyword("_EMISSION"); light.transform.Find("yellow1").GetComponent().material.DisableKeyword("_EMISSION"); light.transform.Find("green2").GetComponent().material.DisableKeyword("_EMISSION"); } } switch (state) { case TrafficLightState.Red: foreach (var light in trafficLights) { if (light != null) { //Texture newTexture = GetTextureForState(state); light.transform.Find("red2").GetComponent().material.EnableKeyword("_EMISSION"); } } foreach (var v in roadEventTrigger) { v.SetActive(true); } break; case TrafficLightState.Yellow: foreach (var light in trafficLights) { if (light != null) { //Texture newTexture = GetTextureForState(state); light.transform.Find("yellow1").GetComponent().material.EnableKeyword("_EMISSION"); } } break; case TrafficLightState.Green: foreach (var light in trafficLights) { if (light != null) { //Texture newTexture = GetTextureForState(state); light.transform.Find("green2").GetComponent().material.EnableKeyword("_EMISSION"); } } foreach (var v in roadEventTrigger) { v.SetActive(false); } // AddRedLightWarning(); break; default: throw new ArgumentOutOfRangeException(nameof(state), state, null); } } } public List lightGroups; void Start() { // 初始化每组红绿灯的计时器 foreach (var group in lightGroups) { group.Initialize(); } } void FixedUpdate() { foreach (var group in lightGroups) { // 更新红绿灯状态 UpdateTrafficLightGroup(group); } } void AddRedLightWarning(TrafficLightGroup group) { Debug.LogWarning("红灯警告:请注意停车!"); } void UpdateTrafficLightGroup(TrafficLightGroup group) { group.timer -= Time.deltaTime; if (group.timer <= 0f) { switch (group.currentState) { case TrafficLightState.Green: group.previousState = TrafficLightState.Green; group.currentState = TrafficLightState.Yellow; group.timer = 2f; // 绿灯到黄灯的持续时间 break; case TrafficLightState.Yellow: group.currentState = TrafficLightState.Red; group.timer = group.startsWithGreen ? 30 : 30; // 黄灯到红灯的持续时间 break; case TrafficLightState.Red: group.previousState = TrafficLightState.Red; group.currentState = TrafficLightState.Green; group.timer = 7; // 红灯到绿灯的持续时间 break; } } // 更新交通灯显示 UpdateLights(group.lights, group.currentState.ToString(), group.timer); group.UpdateTrafficLightTexture(group.currentState); } void UpdateLights(List lights, string state, float time) { int timeInt = Mathf.CeilToInt(time); string formattedTime = timeInt < 10 ? "0" + timeInt.ToString() : timeInt.ToString(); string text = formattedTime; Color color = Color.white; switch (state) { case "Red": color = Color.red; break; case "Yellow": color = Color.yellow; break; case "Green": color = Color.green; break; } foreach (var light in lights) { if (light != null) { light.text = text; light.color = color; } } } }