| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | public class NotificationBadge : MonoBehaviour |
| | 5 | | { |
| | 6 | | [Tooltip("The value shown on this badge is the sum of the notification variables")] |
| | 7 | | [SerializeField] private List<FloatVariable> notificationVariables; |
| | 8 | | [SerializeField] private TMPro.TextMeshProUGUI notificationText; |
| | 9 | | [SerializeField] private GameObject notificationContainer; |
| | 10 | |
|
| 0 | 11 | | public int finalValue { get; private set; } |
| 52 | 12 | | private void Start() { Initialize(); } |
| | 13 | |
|
| | 14 | | public void Initialize() |
| | 15 | | { |
| 28 | 16 | | if (notificationVariables == null || notificationVariables.Count == 0) |
| 0 | 17 | | return; |
| | 18 | |
|
| 122 | 19 | | foreach (var notiVariable in notificationVariables) |
| | 20 | | { |
| 33 | 21 | | notiVariable.OnChange -= NotificationVariable_OnChange; |
| 33 | 22 | | notiVariable.OnChange += NotificationVariable_OnChange; |
| 33 | 23 | | NotificationVariable_OnChange(notiVariable.Get(), notiVariable.Get()); |
| | 24 | | } |
| 28 | 25 | | } |
| | 26 | |
|
| | 27 | | private void OnDestroy() |
| | 28 | | { |
| 28 | 29 | | if (notificationVariables == null || notificationVariables.Count == 0) |
| 0 | 30 | | return; |
| | 31 | |
|
| 122 | 32 | | foreach (var notiVariable in notificationVariables) |
| | 33 | | { |
| 33 | 34 | | notiVariable.OnChange -= NotificationVariable_OnChange; |
| | 35 | | } |
| 28 | 36 | | } |
| | 37 | |
|
| | 38 | | private void NotificationVariable_OnChange(float current, float previous) |
| | 39 | | { |
| 49 | 40 | | int finalValue = 0; |
| | 41 | |
|
| 232 | 42 | | foreach (var notiVariable in notificationVariables) |
| | 43 | | { |
| 67 | 44 | | finalValue += (int)notiVariable.Get(); |
| | 45 | | } |
| | 46 | |
|
| 49 | 47 | | this.finalValue = finalValue; |
| | 48 | |
|
| 49 | 49 | | if (finalValue > 0) |
| | 50 | | { |
| 23 | 51 | | notificationContainer.SetActive(true); |
| | 52 | |
|
| 23 | 53 | | if (finalValue < 99) |
| | 54 | | { |
| 23 | 55 | | notificationText.text = finalValue.ToString(); |
| 23 | 56 | | } |
| | 57 | | else |
| | 58 | | { |
| 0 | 59 | | notificationText.text = "99+"; |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | | else |
| | 63 | | { |
| 26 | 64 | | notificationContainer.SetActive(false); |
| | 65 | | } |
| 26 | 66 | | } |
| | 67 | | } |