using System.Collections; using TMPro; using UnityEngine; enum PrefsType { str, intNumber, floatNumber } [RequireComponent(typeof(TextMeshProUGUI))] public class PrefsApplicator : MonoBehaviour { [SerializeField] PrefsType prefsType = PrefsType.str; [SerializeField] string prefix; [SerializeField] string suffix; [SerializeField] string prefsKey; [SerializeField] string defaultText; Coroutine coroutine; TextMeshProUGUI label; private void OnEnable() => StartPolling(); private void OnDisable() => StopPolling(); void Refresh() { string data; if (PlayerPrefs.HasKey(prefsKey)) { data = prefsType switch { PrefsType.str => PlayerPrefs.GetString(prefsKey), PrefsType.intNumber => PlayerPrefs.GetInt(prefsKey).ToString(), PrefsType.floatNumber => PlayerPrefs.GetFloat(prefsKey).ToString(), _ => PlayerPrefs.GetString(prefsKey) }; } else { data = defaultText; } string message = $"{prefix} {data} {suffix}"; label.text = message; } void StartPolling() { label = GetComponent(); coroutine = StartCoroutine(poll()); } void StopPolling() { if (coroutine != null) StopCoroutine(coroutine); } IEnumerator poll() { while (true) { Refresh(); yield return new WaitForSeconds(5); } } }