GoldPickupNotifier.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. using TMPro;
  3. public class GoldPickupNotifier : MonoBehaviour
  4. {
  5. public static GoldPickupNotifier Instance;
  6. public GameObject messagePanel;
  7. public TextMeshProUGUI messageText;
  8. public float displayDuration = 2f;
  9. public float fadeSpeed = 1f;
  10. private float timer;
  11. private CanvasGroup canvasGroup;
  12. void Awake()
  13. {
  14. if (Instance == null)
  15. {
  16. Instance = this;
  17. }
  18. else
  19. {
  20. Destroy(gameObject);
  21. }
  22. }
  23. void Start()
  24. {
  25. if (messagePanel)
  26. {
  27. canvasGroup = messagePanel.GetComponent<CanvasGroup>();
  28. if (!canvasGroup) canvasGroup = messagePanel.AddComponent<CanvasGroup>();
  29. canvasGroup.alpha = 0;
  30. messagePanel.SetActive(false);
  31. }
  32. }
  33. public static void Show(int goldAmount)
  34. {
  35. if (Instance) Instance.ShowMessage(goldAmount);
  36. }
  37. void ShowMessage(int amount)
  38. {
  39. if (!messagePanel || !messageText) return;
  40. messageText.text = $"+{amount} Gold ";
  41. messagePanel.SetActive(true);
  42. canvasGroup.alpha = 1;
  43. timer = displayDuration;
  44. }
  45. void Update()
  46. {
  47. if (timer > 0)
  48. {
  49. timer -= Time.deltaTime;
  50. if (timer <= fadeSpeed)
  51. {
  52. canvasGroup.alpha = timer / fadeSpeed;
  53. }
  54. if (timer <= 0)
  55. {
  56. messagePanel.SetActive(false);
  57. }
  58. }
  59. }
  60. }