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