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 void Initialize() { // 绿灯开始则设为10秒,否则设为39秒 currentState = startsWithGreen ? TrafficLightState.Green : TrafficLightState.Red; previousState = currentState; // 初始化时,之前的状态与当前状态相同 timer = startsWithGreen ? 10f : 39; } 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"); } } 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"); } } AddRedLightWarning(); break; default: throw new ArgumentOutOfRangeException(nameof(state), state, null); } } // private Texture GetTextureForState(TrafficLightState state) // { // // 返回根据 state 获取的贴图 // // 这里你需要根据你的资源来实现具体的逻辑 // } } public List lightGroups; void Start() { // 初始化每组红绿灯的计时器 foreach (var group in lightGroups) { group.Initialize(); } } void FixedUpdate() { // Debug.Log("Delta Time: " + Time.deltaTime); foreach (var group in lightGroups) { // Debug.Log("Before: " + group.timer); // group.timer -= Time.deltaTime; // Debug.Log("After: " + group.timer); // 更新红绿灯状态 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 = 3f; // 绿灯到黄灯的持续时间 break; case TrafficLightState.Yellow: group.currentState = TrafficLightState.Red; group.timer = group.startsWithGreen ? 39 : 20; // 黄灯到红灯的持续时间 break; case TrafficLightState.Red: group.previousState = TrafficLightState.Red; group.currentState = TrafficLightState.Green; group.timer = 10f; // 红灯到绿灯的持续时间 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); // 使用条件运算符来判断是否需要在前面加上 0 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; } } } }