FM/Assets/Scripts/Base/Language/LanguageItem.cs

113 lines
3.8 KiB
C#
Raw Normal View History

2025-05-23 16:24:00 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2025-08-25 21:05:45 +08:00
using HK;
2025-05-23 16:24:00 +08:00
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class LanguageItem : MonoBehaviour
{
2025-06-04 22:49:37 +08:00
public TMP_Text Text;
2025-05-23 16:24:00 +08:00
2025-06-04 22:49:37 +08:00
public bool isSetText = false;
[ConditionalField("isSetText")] public string chin;
[ConditionalField("isSetText")] public string english;
[ConditionalField("isSetText")] public TMP_FontAsset ChinFontAsset;
[ConditionalField("isSetText")] public TMP_FontAsset EnglishFontAsset;
2025-05-23 16:24:00 +08:00
2025-06-04 22:49:37 +08:00
public bool isSetImage = false;
[ConditionalField("isSetImage")] public Image ChinImage;
[ConditionalField("isSetImage")] public Image EnglishImage;
2025-05-23 16:24:00 +08:00
2025-06-04 22:49:37 +08:00
public bool isSetGo = false;
[ConditionalField("isSetGo")] public GameObject ChinGo;
[ConditionalField("isSetGo")] public GameObject EnglishGo;
2025-05-23 16:24:00 +08:00
2025-08-25 21:05:45 +08:00
public bool isSetSprite = false;
[ConditionalField("isSetSprite")] public string ChinSpritePath;
[ConditionalField("isSetSprite")] public string EnglishSpritePath;
2025-06-17 09:31:12 +08:00
private LanguageManager.LanguageType currentType = LanguageManager.LanguageType.English;
2025-05-23 16:24:00 +08:00
private void Awake()
{
2025-08-25 21:05:45 +08:00
if (isSetText)
Text = GetComponentInParent<TMP_Text>();
2025-05-23 16:24:00 +08:00
}
private void Start()
{
LanguageManager.Instance.LanguageChange += LanguageChange;
2025-07-02 10:24:01 +08:00
LanguageChange(LanguageManager.Instance.type);
2025-08-22 22:37:04 +08:00
if (LanguageManager.Instance.IsSimplified)
2025-05-23 16:24:00 +08:00
chin = LanguageManager.Instance.T2S(chin);
}
2025-07-02 10:24:01 +08:00
private void OnDestroy()
{
LanguageManager.Instance.LanguageChange -= LanguageChange;
}
2025-05-23 16:24:00 +08:00
private void LanguageChange(LanguageManager.LanguageType type)
{
2025-06-17 09:31:12 +08:00
currentType = type;
2025-05-23 16:24:00 +08:00
switch (type)
{
case LanguageManager.LanguageType.English:
2025-06-04 22:49:37 +08:00
if (isSetImage && ChinImage != null && EnglishImage != null)
2025-05-23 16:24:00 +08:00
{
ChinImage.gameObject.SetActive(false);
EnglishImage.gameObject.SetActive(true);
}
2025-06-04 22:49:37 +08:00
if (isSetText && Text != null)
2025-05-23 16:24:00 +08:00
{
2025-08-22 22:37:04 +08:00
if (!string.IsNullOrEmpty(english))
Text.text = english;
2025-05-23 16:24:00 +08:00
Text.font = EnglishFontAsset;
}
2025-06-04 22:49:37 +08:00
if (isSetGo && ChinGo != null && EnglishGo != null)
{
ChinGo.gameObject.SetActive(false);
EnglishGo.gameObject.SetActive(true);
}
2025-08-25 21:05:45 +08:00
if (isSetSprite && !string.IsNullOrEmpty(ChinSpritePath) && !string.IsNullOrEmpty(EnglishSpritePath))
{
GetComponentInParent<Image>().sprite = ResourcesManager.Instance.Load<Sprite>(EnglishSpritePath);
}
2025-05-23 16:24:00 +08:00
break;
case LanguageManager.LanguageType.Chinese:
2025-06-04 22:49:37 +08:00
if (isSetImage && ChinImage != null && EnglishImage != null)
2025-05-23 16:24:00 +08:00
{
ChinImage.gameObject.SetActive(true);
EnglishImage.gameObject.SetActive(false);
}
2025-06-04 22:49:37 +08:00
if (isSetText && Text != null)
2025-05-23 16:24:00 +08:00
{
2025-08-22 22:37:04 +08:00
if (!string.IsNullOrEmpty(chin))
Text.text = chin;
2025-05-23 16:24:00 +08:00
Text.font = ChinFontAsset;
}
2025-06-04 22:49:37 +08:00
if (isSetGo && ChinGo != null && EnglishGo != null)
{
ChinGo.gameObject.SetActive(true);
EnglishGo.gameObject.SetActive(false);
}
2025-08-25 21:05:45 +08:00
if (isSetSprite && !string.IsNullOrEmpty(ChinSpritePath) && !string.IsNullOrEmpty(EnglishSpritePath))
{
GetComponentInParent<Image>().sprite = ResourcesManager.Instance.Load<Sprite>(ChinSpritePath);
}
2025-05-23 16:24:00 +08:00
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
}