2024-11-21 20:36:16 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
public class LightBlink : MonoBehaviour
|
|
|
|
|
{
|
2024-11-22 17:48:26 +08:00
|
|
|
|
public Image lightImage; // <20>ƹ<EFBFBD>ͼƬ
|
|
|
|
|
private float minBlinkInterval = 0.2f; // ÿ<><C3BF><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
|
|
|
|
|
private float maxBlinkInterval = 0.6f; // ÿ<><C3BF><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
public float minCooldown = 5f; // <20><>С<EFBFBD><D0A1>ȴʱ<C8B4><CAB1>
|
|
|
|
|
public float maxCooldown = 10f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴʱ<C8B4><CAB1>
|
|
|
|
|
|
|
|
|
|
private float nextBlinkTime; // <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˸ʱ<CBB8><CAB1><EFBFBD><EFBFBD>
|
|
|
|
|
private int remainingBlinks; // <20><>ǰʣ<C7B0><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD>
|
|
|
|
|
private bool isInCooldown; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ״̬
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
// ȷ<><C8B7><EFBFBD>ƹ<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD>
|
|
|
|
|
if (lightImage != null)
|
|
|
|
|
{
|
|
|
|
|
lightImage.enabled = true;
|
|
|
|
|
}
|
|
|
|
|
EnterCooldown();
|
|
|
|
|
}
|
2024-11-21 20:36:16 +08:00
|
|
|
|
|
|
|
|
|
private void Update()
|
2024-11-22 17:48:26 +08:00
|
|
|
|
{
|
|
|
|
|
if (lightImage == null) return;
|
|
|
|
|
|
|
|
|
|
if (isInCooldown && Time.time >= nextBlinkTime)
|
|
|
|
|
{
|
|
|
|
|
StartBlinkSequence();
|
|
|
|
|
}
|
|
|
|
|
else if (!isInCooldown && Time.time >= nextBlinkTime)
|
|
|
|
|
{
|
|
|
|
|
PerformBlink();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartBlinkSequence()
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32>3<EFBFBD>Σ<EFBFBD>
|
|
|
|
|
remainingBlinks = Random.Range(2, 4);
|
|
|
|
|
isInCooldown = false;
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˸
|
|
|
|
|
PerformBlink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PerformBlink()
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD>ݹرյƹ⣬<C6B9>ٴ<EFBFBD><D9B4><EFBFBD>
|
|
|
|
|
lightImage.enabled = false;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD>
|
|
|
|
|
remainingBlinks--;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ƹ<EFBFBD>
|
|
|
|
|
Invoke("TurnOnLight", Random.Range(minBlinkInterval, maxBlinkInterval));
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>꣬<EFBFBD><EAA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ
|
|
|
|
|
if (remainingBlinks <= 0)
|
|
|
|
|
{
|
|
|
|
|
EnterCooldown();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˸ʱ<CBB8><CAB1>
|
|
|
|
|
ScheduleNextBlink(minBlinkInterval, maxBlinkInterval);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TurnOnLight()
|
2024-11-21 20:36:16 +08:00
|
|
|
|
{
|
|
|
|
|
if (lightImage != null)
|
|
|
|
|
{
|
2024-11-22 17:48:26 +08:00
|
|
|
|
lightImage.enabled = true;
|
2024-11-21 20:36:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-22 17:48:26 +08:00
|
|
|
|
|
|
|
|
|
private void EnterCooldown()
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴʱ<C8B4><CAB1>
|
|
|
|
|
isInCooldown = true;
|
|
|
|
|
lightImage.enabled = true; // ȷ<><C8B7><EFBFBD>ƹⱣ<C6B9><E2B1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
ScheduleNextBlink(minCooldown, maxCooldown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ScheduleNextBlink(float minTime, float maxTime)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
float interval = Random.Range(minTime, maxTime);
|
|
|
|
|
nextBlinkTime = Time.time + interval;
|
|
|
|
|
}
|
|
|
|
|
}
|