| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using UnityEngine;
- using TMPro;
- public class GoldPickupNotifier : MonoBehaviour
- {
- public static GoldPickupNotifier Instance;
-
- public GameObject messagePanel;
- public TextMeshProUGUI messageText;
- public float displayDuration = 2f;
- public float fadeSpeed = 1f;
-
- private float timer;
- private CanvasGroup canvasGroup;
-
- void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- }
- else
- {
- Destroy(gameObject);
- }
- }
-
- void Start()
- {
- if (messagePanel)
- {
- canvasGroup = messagePanel.GetComponent<CanvasGroup>();
- if (!canvasGroup) canvasGroup = messagePanel.AddComponent<CanvasGroup>();
- canvasGroup.alpha = 0;
- messagePanel.SetActive(false);
- }
- }
-
- public static void Show(int goldAmount)
- {
- if (Instance) Instance.ShowMessage(goldAmount);
- }
-
- void ShowMessage(int amount)
- {
- if (!messagePanel || !messageText) return;
-
- messageText.text = $"+{amount} Gold ";
- messagePanel.SetActive(true);
- canvasGroup.alpha = 1;
- timer = displayDuration;
- }
-
- void Update()
- {
- if (timer > 0)
- {
- timer -= Time.deltaTime;
-
- if (timer <= fadeSpeed)
- {
- canvasGroup.alpha = timer / fadeSpeed;
- }
-
- if (timer <= 0)
- {
- messagePanel.SetActive(false);
- }
- }
- }
- }
|