54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GridColor : MonoBehaviour
|
|
{
|
|
public Image cellImage;
|
|
public Sprite defaultSprite;
|
|
public Sprite whiteSprite;
|
|
public Sprite blueSprite;
|
|
public Sprite orangeSprite;
|
|
public Sprite redSprite;
|
|
public Sprite greenSprite;
|
|
public int cellX;
|
|
public int cellY;
|
|
public float flashDuration = 0.3f;
|
|
|
|
private float flashEnd;
|
|
private bool colored; // 目前顯示的是判定色(優先於按住時的白色)
|
|
|
|
void Awake()
|
|
{
|
|
if (cellImage == null) cellImage = GetComponent<Image>();
|
|
if (defaultSprite == null) defaultSprite = cellImage.sprite;
|
|
enabled = false; // 沒有閃爍時不跑 Update
|
|
}
|
|
|
|
// 按住提示:不覆蓋判定色
|
|
public void FlashWhite()
|
|
{
|
|
if (enabled && colored) return;
|
|
Flash(whiteSprite, false);
|
|
}
|
|
|
|
public void FlashBlue() => Flash(blueSprite, true);
|
|
public void FlashOrange() => Flash(orangeSprite, true);
|
|
public void FlashRed() => Flash(redSprite, true);
|
|
public void FlashGreen() => Flash(greenSprite, true);
|
|
|
|
private void Flash(Sprite sprite, bool isColor)
|
|
{
|
|
cellImage.sprite = sprite;
|
|
colored = isColor;
|
|
flashEnd = Time.time + flashDuration;
|
|
enabled = true;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Time.time < flashEnd) return;
|
|
cellImage.sprite = defaultSprite;
|
|
enabled = false;
|
|
}
|
|
}
|