Danro/Assets/Scripts/UI/UICreditsTextController.cs

54 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using RR.Common.Managers;
using RR.Game2D.Core.Managers;
using TMPro;
using UnityEngine;
public class UICreditsTextController : MonoBehaviour
{
// UI elements
[SerializeField] private TextMeshProUGUI creditsText;
[SerializeField] private TextMeshProUGUI livesText;
// Strings to display
[SerializeField] private string creditsString = "У вас {0} кредитов";
[SerializeField] private string livesString = "У вас будет {0}";
private void Start()
{
PaymentManager paymentManager = PaymentManager.Instance;
GameManager gameManager = GameManager.Instance;
// Subscribe to events
if (paymentManager != null)
{
UpdateCreditsText(paymentManager.CreditsCount);
paymentManager.OnCreditsChanged.AddListener(UpdateCreditsText);
}
if (gameManager != null)
{
UpdateLivesText(gameManager.MaxLives);
gameManager.MaxLivesChanged += (UpdateLivesText);
}
}
private void OnDestroy()
{
PaymentManager paymentManager = PaymentManager.Instance;
GameManager gameManager = GameManager.Instance;
if (paymentManager != null) paymentManager.OnCreditsChanged.RemoveListener(UpdateCreditsText);
if (gameManager != null) gameManager.MaxLivesChanged -= UpdateLivesText;
}
private void UpdateCreditsText(int creditsCount)
{
creditsText.text = string.Format(creditsString, creditsCount);
}
private void UpdateLivesText(int newLives)
{
livesText.text = string.Format(livesString, newLives);
}
}