This commit is contained in:
yzx 2023-12-26 13:16:56 +08:00
parent 585c819adf
commit 234b98c440
2 changed files with 1000 additions and 22 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,15 +4,17 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
public enum TrafficLightState public enum TrafficLightState
{ {
Red, Red,
Yellow, Yellow,
Green Green
} }
public class TrafficLightManager : MonoBehaviour public class TrafficLightManager : MonoBehaviour
{ {
[System.Serializable] [System.Serializable]
public class TrafficLightGroup public class TrafficLightGroup
{ {
public List<TMP_Text> lights = new List<TMP_Text>(); public List<TMP_Text> lights = new List<TMP_Text>();
@ -30,23 +32,22 @@ public class TrafficLightManager : MonoBehaviour
timer = startsWithGreen ? 10f : 39; timer = startsWithGreen ? 10f : 39;
} }
public void UpdateTrafficLightTexture(TrafficLightState state) public void UpdateTrafficLightTexture(TrafficLightState state)
{ {
Debug.Log("123");
Debug.Log("123"); foreach (var light in trafficLights)
{
foreach (var light in trafficLights) if (light != null)
{ {
if (light != null) // 假设你有一个方法来根据状态获取相应的贴图
{ //Texture newTexture = GetTextureForState(state);
// 假设你有一个方法来根据状态获取相应的贴图 light.transform.Find("red2").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION");
//Texture newTexture = GetTextureForState(state); light.transform.Find("yellow1").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION");
light.transform.Find("red2").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION"); light.transform.Find("green2").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION");
light.transform.Find("yellow1").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION"); }
light.transform.Find("green2").GetComponent<MeshRenderer>().material.DisableKeyword("_EMISSION"); }
}
}
switch (state) switch (state)
{ {
@ -60,6 +61,7 @@ foreach (var light in trafficLights)
light.transform.Find("red2").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION"); light.transform.Find("red2").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
} }
} }
break; break;
case TrafficLightState.Yellow: case TrafficLightState.Yellow:
foreach (var light in trafficLights) foreach (var light in trafficLights)
@ -71,6 +73,7 @@ foreach (var light in trafficLights)
light.transform.Find("yellow1").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION"); light.transform.Find("yellow1").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
} }
} }
break; break;
case TrafficLightState.Green: case TrafficLightState.Green:
foreach (var light in trafficLights) foreach (var light in trafficLights)
@ -82,11 +85,11 @@ foreach (var light in trafficLights)
light.transform.Find("green2").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION"); light.transform.Find("green2").GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
} }
} }
break; break;
default: default:
throw new ArgumentOutOfRangeException(nameof(state), state, null); throw new ArgumentOutOfRangeException(nameof(state), state, null);
} }
} }
// private Texture GetTextureForState(TrafficLightState state) // private Texture GetTextureForState(TrafficLightState state)
@ -105,7 +108,6 @@ foreach (var light in trafficLights)
{ {
group.Initialize(); group.Initialize();
} }
} }
@ -117,7 +119,7 @@ foreach (var light in trafficLights)
Debug.Log("Before: " + group.timer); Debug.Log("Before: " + group.timer);
// group.timer -= Time.deltaTime; // group.timer -= Time.deltaTime;
Debug.Log("After: " + group.timer); Debug.Log("After: " + group.timer);
// 更新红绿灯状态 // 更新红绿灯状态
UpdateTrafficLightGroup(group); UpdateTrafficLightGroup(group);
} }
@ -157,15 +159,12 @@ foreach (var light in trafficLights)
} }
void UpdateLights(List<TMP_Text> lights, string state, float time) void UpdateLights(List<TMP_Text> lights, string state, float time)
{ {
int timeInt = Mathf.CeilToInt(time); int timeInt = Mathf.CeilToInt(time);
// 使用条件运算符来判断是否需要在前面加上 0 // 使用条件运算符来判断是否需要在前面加上 0
string formattedTime = timeInt < 10 ? "0" + timeInt.ToString() : timeInt.ToString(); string formattedTime = timeInt < 10 ? "0" + timeInt.ToString() : timeInt.ToString();
string text = formattedTime; string text = formattedTime;
Color color = Color.white; Color color = Color.white;
switch (state) switch (state)