41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class InitialTrafficLightSequence : MonoBehaviour
|
|
{
|
|
public List<TMP_Text> trafficLightText; // 用于显示红绿灯状态的文本
|
|
public TrafficLightManager trafficLightManager; // 引用主交通灯管理器
|
|
|
|
private float initialRedTimer = 3.0f; // 初始红灯持续时间
|
|
|
|
void Start()
|
|
{
|
|
// 设置初始红灯
|
|
UpdateLight("红灯", initialRedTimer);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (initialRedTimer > 0)
|
|
{
|
|
initialRedTimer -= Time.deltaTime;
|
|
UpdateLight("红灯", initialRedTimer);
|
|
}
|
|
else
|
|
{
|
|
// 红灯结束,启动常规交通灯循环
|
|
trafficLightManager.enabled = true;
|
|
this.enabled = false; // 关闭当前脚本,避免重复调用
|
|
}
|
|
}
|
|
|
|
void UpdateLight(string state, float time)
|
|
{
|
|
foreach (var v in trafficLightText)
|
|
{
|
|
v.text = $"{Mathf.CeilToInt(time)}";
|
|
v.color = state == "红灯" ? Color.red : Color.green;
|
|
}
|
|
}
|
|
} |