Files
2026-03-04 09:37:33 +08:00

350 lines
9.4 KiB
C#

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<Item> items = new List<Item>();
private List<Animator> timers = new List<Animator>();
[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<LocalizedObject>();
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<TextMeshProUGUI>();
if (!useLocalization || string.IsNullOrEmpty(items[i].titleKey))
{
itemTitle.text = items[i].title;
}
else
{
LocalizedObject tempLoc = itemTitle.GetComponent<LocalizedObject>();
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<TextMeshProUGUI>();
if (!useLocalization || string.IsNullOrEmpty(items[i].descriptionKey))
{
itemDescription.text = items[i].description;
}
else
{
LocalizedObject tempLoc2 = itemDescription.GetComponent<LocalizedObject>();
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<Image>().sprite = items[i].background;
ButtonManager libraryItemButton = gameObject.GetComponentInChildren<ButtonManager>();
if (libraryItemButton != null)
{
if (!useLocalization)
{
libraryItemButton.buttonText = items[i].buttonText;
}
else
{
LocalizedObject tempLoc3 = libraryItemButton.GetComponent<LocalizedObject>();
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<Animator>());
timerGO.transform.Find("Dot").GetComponent<Button>().onClick.AddListener(delegate
{
StopCoroutine("WaitForSliderTimer");
StopCoroutine("DisableItemAnimators");
currentItemObject.gameObject.SetActive(value: true);
currentItemObject.enabled = true;
currentIndicatorObject.enabled = true;
currentItemObject.Play("Out");
currentIndicatorObject.Play("Out");
currentSliderIndex = timerGO.transform.GetSiblingIndex();
currentItemObject = itemParent.GetChild(currentSliderIndex).GetComponent<Animator>();
currentIndicatorBar = timers[currentSliderIndex].transform.Find("Bar/Filled").GetComponent<Image>();
currentIndicatorObject = timers[currentSliderIndex];
currentItemObject.gameObject.SetActive(value: true);
currentItemObject.enabled = true;
currentIndicatorObject.enabled = true;
currentItemObject.Play("In");
currentIndicatorObject.Play("In");
sliderTimerBar = 0f;
currentIndicatorBar.fillAmount = sliderTimerBar;
StartCoroutine("WaitForSliderTimer");
StartCoroutine("DisableItemAnimators");
});
}
isInitialized = true;
StartCoroutine(PrepareSlider());
}
public void AllowUpdate(bool canUpdate)
{
allowUpdate = canUpdate;
if (base.gameObject.activeInHierarchy)
{
if (!allowUpdate)
{
StopCoroutine("WaitForSliderTimer");
}
else
{
StartCoroutine("WaitForSliderTimer");
}
}
}
private IEnumerator PrepareSlider()
{
if (updateMode == UpdateMode.UnscaledTime)
{
yield return new WaitForSecondsRealtime(0.02f);
}
else
{
yield return new WaitForSeconds(0.02f);
}
sliderTimerBar = 0f;
currentItemObject = itemParent.GetChild(currentSliderIndex).GetComponent<Animator>();
currentIndicatorBar = timers[currentSliderIndex].transform.Find("Bar/Filled").GetComponent<Image>();
currentIndicatorObject = timers[currentSliderIndex];
currentItemObject.enabled = true;
currentIndicatorObject.enabled = true;
currentItemObject.Play("In");
currentIndicatorObject.Play("In");
StopCoroutine("WaitForSliderTimer");
StopCoroutine("DisableItemAnimators");
StartCoroutine("WaitForSliderTimer");
StartCoroutine("DisableItemAnimators");
}
private IEnumerator WaitForSliderTimer()
{
if (updateMode == UpdateMode.UnscaledTime)
{
yield return new WaitForSecondsRealtime(sliderTimer);
}
else
{
yield return new WaitForSeconds(sliderTimer);
}
if (!allowUpdate)
{
StartCoroutine("WaitForSliderTimer");
yield break;
}
currentItemObject.enabled = true;
currentIndicatorObject.enabled = true;
currentItemObject.Play("Out");
currentIndicatorObject.Play("Out");
if (currentSliderIndex == items.Count - 1)
{
currentSliderIndex = 0;
}
else
{
currentSliderIndex++;
}
sliderTimerBar = 0f;
currentItemObject = itemParent.GetChild(currentSliderIndex).GetComponent<Animator>();
currentIndicatorBar = timers[currentSliderIndex].transform.Find("Bar/Filled").GetComponent<Image>();
currentIndicatorBar.fillAmount = sliderTimerBar;
currentIndicatorObject = timers[currentSliderIndex];
currentItemObject.gameObject.SetActive(value: true);
currentItemObject.enabled = true;
currentIndicatorObject.enabled = true;
currentItemObject.Play("In");
currentIndicatorObject.Play("In");
StartCoroutine("DisableItemAnimators");
StartCoroutine("WaitForSliderTimer");
}
private IEnumerator DisableItemAnimators()
{
if (updateMode == UpdateMode.UnscaledTime)
{
yield return new WaitForSecondsRealtime(0.6f);
}
else
{
yield return new WaitForSeconds(0.6f);
}
for (int i = 0; i < items.Count; i++)
{
if (i != currentSliderIndex)
{
itemParent.GetChild(i).gameObject.SetActive(value: false);
}
itemParent.GetChild(i).GetComponent<Animator>().enabled = false;
timers[i].enabled = false;
}
}
}
}