| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using AppUI.Config;
- using AppUI.Localization;
- using TMPro;
- using UnityEngine;
- namespace AppUI.Component.DeviceInfo
- {
- /// <summary>
- /// 版本行等需要运行时参数(平台、版本号)的文案。
- /// 与同一 TMP 上的 <see cref="AppUITextAutoLanguage"/> 配合:Prefab 配 textKey,本脚本只填 <see cref="AppUITextAutoLanguage.textFormatArgs"/>。
- /// </summary>
- [ExecuteAlways]
- public class DeviceInfo : MonoBehaviour
- {
- const string VersionTextKey = "appui-login-version";
- [SerializeField]
- TMP_Text deviceInfo;
- AppUITextAutoLanguage _versionLocalized;
- void Awake()
- {
- ResolveReferences();
- }
- void OnEnable()
- {
- AppUILocalization.OnLanguageChanged += RefreshVersionText;
- RefreshVersionText(AppUILocalization.GetLanguage());
- }
- void Start()
- {
- RefreshVersionText(AppUILocalization.GetLanguage());
- }
- void OnDisable()
- {
- AppUILocalization.OnLanguageChanged -= RefreshVersionText;
- }
- #if UNITY_EDITOR
- void OnValidate()
- {
- ResolveReferences();
- if (!Application.isPlaying)
- RefreshVersionText(AppUILocalization.GetLanguage());
- }
- #endif
- void ResolveReferences()
- {
- if (deviceInfo == null)
- deviceInfo = GetComponentInChildren<TMP_Text>(true);
- if (deviceInfo != null)
- _versionLocalized = deviceInfo.GetComponent<AppUITextAutoLanguage>();
- }
- void RefreshVersionText(LanguageEnum _)
- {
- string platform = GlobalConfig.GetPlatformDisplayName();
- string version = Application.version;
- #if UNITY_EDITOR
- if (!Application.isPlaying && string.IsNullOrEmpty(version))
- version = "1.0.0";
- #endif
- if (_versionLocalized != null)
- {
- _versionLocalized.textFormatArgs = new object[] { platform, version };
- if (string.IsNullOrEmpty(_versionLocalized.GetTextKey()))
- _versionLocalized.SetTextKey(VersionTextKey);
- else
- _versionLocalized.ApplyToText();
- return;
- }
- if (deviceInfo == null)
- return;
- deviceInfo.text = string.Format(
- AppUILocalization.GetTextByKey(VersionTextKey),
- platform,
- version);
- }
- }
- }
|