using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; namespace Michsky.UI.Heat { public class NewsSlider : MonoBehaviour { public enum UpdateMode { DeltaTime = 0, UnscaledTime = 1 } [Serializable] public class Item { public string title = "News title"; [TextArea] public string description = "News description"; public Sprite background; public string buttonText = "Show More"; public UnityEvent onButtonClick = new UnityEvent(); [Header("Localization")] public string titleKey = "TitleKey"; public string descriptionKey = "DescriptionKey"; public string buttonTextKey = "ButtonTextKey"; } public List items = new List(); private List timers = new List(); [SerializeField] private GameObject itemPreset; [SerializeField] private Transform itemParent; [SerializeField] private GameObject timerPreset; [SerializeField] private Transform timerParent; public bool allowUpdate = true; public bool useLocalization = true; [Range(1f, 30f)] public float sliderTimer = 4f; [SerializeField] private UpdateMode updateMode; private Animator currentItemObject; private Animator currentIndicatorObject; private Image currentIndicatorBar; private int currentSliderIndex; private float sliderTimerBar; private bool isInitialized; private LocalizedObject localizedObject; private void OnEnable() { if (!isInitialized) { Initialize(); } else { StartCoroutine(PrepareSlider()); } } private void Update() { if (isInitialized && allowUpdate) { CheckForTimer(); } } private void CheckForTimer() { if (sliderTimerBar <= sliderTimer && currentIndicatorBar != null) { if (updateMode == UpdateMode.UnscaledTime) { sliderTimerBar += Time.unscaledDeltaTime; } else { sliderTimerBar += Time.deltaTime; } currentIndicatorBar.fillAmount = sliderTimerBar / sliderTimer; } } public void Initialize() { if (useLocalization) { localizedObject = base.gameObject.GetComponent(); if (localizedObject == null || !localizedObject.CheckLocalizationStatus()) { useLocalization = false; } } foreach (Transform item in itemParent) { UnityEngine.Object.Destroy(item.gameObject); } foreach (Transform item2 in timerParent) { UnityEngine.Object.Destroy(item2.gameObject); } for (int i = 0; i < items.Count; i++) { int tempIndex = i; GameObject gameObject = UnityEngine.Object.Instantiate(itemPreset, new Vector3(0f, 0f, 0f), Quaternion.identity); gameObject.transform.SetParent(itemParent, worldPositionStays: false); gameObject.gameObject.name = items[i].title; TextMeshProUGUI itemTitle = gameObject.transform.Find("Title").GetComponent(); if (!useLocalization || string.IsNullOrEmpty(items[i].titleKey)) { itemTitle.text = items[i].title; } else { LocalizedObject tempLoc = itemTitle.GetComponent(); if (tempLoc != null) { tempLoc.tableIndex = localizedObject.tableIndex; tempLoc.localizationKey = items[i].titleKey; tempLoc.onLanguageChanged.AddListener(delegate { itemTitle.text = tempLoc.GetKeyOutput(tempLoc.localizationKey); }); tempLoc.InitializeItem(); tempLoc.UpdateItem(); } } TextMeshProUGUI itemDescription = gameObject.transform.Find("Description").GetComponent(); if (!useLocalization || string.IsNullOrEmpty(items[i].descriptionKey)) { itemDescription.text = items[i].description; } else { LocalizedObject tempLoc2 = itemDescription.GetComponent(); if (tempLoc2 != null) { tempLoc2.tableIndex = localizedObject.tableIndex; tempLoc2.localizationKey = items[i].descriptionKey; tempLoc2.onLanguageChanged.AddListener(delegate { itemDescription.text = tempLoc2.GetKeyOutput(tempLoc2.localizationKey); }); tempLoc2.InitializeItem(); tempLoc2.UpdateItem(); } } gameObject.transform.Find("Background").GetComponent().sprite = items[i].background; ButtonManager libraryItemButton = gameObject.GetComponentInChildren(); if (libraryItemButton != null) { if (!useLocalization) { libraryItemButton.buttonText = items[i].buttonText; } else { LocalizedObject tempLoc3 = libraryItemButton.GetComponent(); if (tempLoc3 != null) { tempLoc3.tableIndex = localizedObject.tableIndex; tempLoc3.localizationKey = items[i].buttonTextKey; tempLoc3.onLanguageChanged.AddListener(delegate { libraryItemButton.buttonText = tempLoc3.GetKeyOutput(tempLoc3.localizationKey); libraryItemButton.UpdateUI(); }); tempLoc3.InitializeItem(); tempLoc3.UpdateItem(); } } libraryItemButton.UpdateUI(); libraryItemButton.onClick.AddListener(delegate { items[tempIndex].onButtonClick.Invoke(); }); if (string.IsNullOrEmpty(libraryItemButton.buttonText)) { libraryItemButton.gameObject.SetActive(value: false); } } GameObject timerGO = UnityEngine.Object.Instantiate(timerPreset, new Vector3(0f, 0f, 0f), Quaternion.identity); timerGO.transform.SetParent(timerParent, worldPositionStays: false); timerGO.gameObject.name = items[i].title; timers.Add(timerGO.GetComponent()); timerGO.transform.Find("Dot").GetComponent