Skip to content

Commit 15b3568

Browse files
committed
localize text group 추가
1 parent 9808d8b commit 15b3568

File tree

5 files changed

+180
-13
lines changed

5 files changed

+180
-13
lines changed

Runtime/Component/LocalizeComponentBase.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public void SetLanguageKey(string key)
3535

3636
_languageKey = key;
3737
_languageParam = Array.Empty<string>();
38-
OnChangeLanguage(s_manager.CurrentLanguage);
38+
39+
if (s_manager != null)
40+
{
41+
OnChangeLanguage(s_manager.CurrentLanguage);
42+
}
3943
}
4044

4145
public void SetLanguageKeyWithParam(string key, params string[] param)
@@ -48,7 +52,11 @@ public void SetLanguageKeyWithParam(string key, params string[] param)
4852

4953
_languageKey = key;
5054
_languageParam = param;
51-
OnChangeLanguage(s_manager.CurrentLanguage);
55+
56+
if (s_manager != null)
57+
{
58+
OnChangeLanguage(s_manager.CurrentLanguage);
59+
}
5260
}
5361

5462
public void UpdateLocalize()
@@ -84,7 +92,7 @@ IEnumerator OnEnableCoroutine()
8492
OnSetup();
8593
}
8694

87-
void OnDisable()
95+
protected virtual void OnDisable()
8896
{
8997
if (s_isAppQuitting)
9098
return;

Runtime/Component/LocalizeText.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ protected override void OnChangeLanguage(SystemLanguage language)
4848
}
4949
}
5050

51+
protected override void OnDisable()
52+
{
53+
base.OnDisable();
54+
55+
if (s_manager != null)
56+
{
57+
s_manager.OnChangeFont -= OnChangeFont;
58+
}
59+
}
60+
5161
void OnChangeFont(Font font)
5262
{
5363
TextComponent.font = font;
5464
}
5565
}
56-
57-
#if UNITY_EDITOR
58-
// [CustomEditor(typeof(LocalizeText))]
59-
// public abstract class LocalizeText_Inspector : LocalizeComponentBase_Inspector<LocalizeText>
60-
// {
61-
// }
62-
#endif
6366
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System.Linq;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
#if UNITY_EDITOR
6+
using UnityEditor;
7+
#endif
8+
9+
namespace UNKO.Localize
10+
{
11+
public class LocalizeTextGroup : LocalizeComponentBase
12+
{
13+
static string[] _dummyParam = new string[0];
14+
15+
[SerializeField]
16+
int _groupIndex;
17+
18+
[SerializeField]
19+
private Text _text; public Text TextComponent { get { Awake(); return _text; } }
20+
public string Text { get => TextComponent.text; set => TextComponent.text = value; }
21+
22+
bool _isExecuteAwake = false;
23+
24+
public void Awake()
25+
{
26+
if (_isExecuteAwake)
27+
{
28+
return;
29+
}
30+
_isExecuteAwake = true;
31+
32+
if (_text == null)
33+
_text = GetComponent<Text>();
34+
}
35+
36+
public string GetGroupText()
37+
=> GetGroupText(0);
38+
39+
public string GetGroupText(int groupIndex)
40+
{
41+
Awake();
42+
43+
string key = _languageKey;
44+
if (groupIndex > 0)
45+
{
46+
key += $"_{groupIndex}";
47+
}
48+
49+
_languageParam = _dummyParam;
50+
return s_manager.GetLocalizeText(key);
51+
}
52+
53+
public string GetGroupTextWithParam(params string[] param)
54+
=> GetGroupTextWithParam(0, param);
55+
56+
public string GetGroupTextWithParam(int groupIndex, params string[] param)
57+
{
58+
Awake();
59+
60+
string key = _languageKey;
61+
if (groupIndex > 0)
62+
{
63+
key += $"_{groupIndex}";
64+
}
65+
66+
_languageParam = param;
67+
return s_manager.GetLocalizeText(key, param);
68+
}
69+
70+
public void SetGroupText()
71+
=> SetGroupText(0);
72+
73+
public void SetGroupText(int groupIndex)
74+
{
75+
TextComponent.text = GetGroupText(groupIndex);
76+
_groupIndex = groupIndex;
77+
}
78+
79+
public void SetGroupTextWithParam(params string[] param)
80+
=> SetGroupTextWithParam(0, param);
81+
82+
public void SetGroupTextWithParam(int groupIndex, params string[] param)
83+
{
84+
TextComponent.text = GetGroupTextWithParam(groupIndex, param);
85+
_groupIndex = groupIndex;
86+
}
87+
88+
protected override void OnSetup()
89+
{
90+
base.OnSetup();
91+
92+
s_manager.OnChangeFont -= OnChangeFont;
93+
s_manager.OnChangeFont += OnChangeFont;
94+
95+
if (s_manager.TryGetFont(out Font font))
96+
{
97+
OnChangeFont(font);
98+
}
99+
}
100+
101+
protected override void OnChangeLanguage(SystemLanguage language)
102+
{
103+
if (string.IsNullOrEmpty(_languageKey))
104+
return;
105+
106+
if (TextComponent == null)
107+
{
108+
return;
109+
}
110+
111+
if (_languageParam.Length > 0)
112+
{
113+
TextComponent.text = s_manager.GetLocalizeText(_languageKey, _languageParam);
114+
}
115+
else
116+
{
117+
TextComponent.text = s_manager.GetLocalizeText(_languageKey);
118+
}
119+
}
120+
protected override void OnDisable()
121+
{
122+
base.OnDisable();
123+
124+
if (s_manager != null)
125+
{
126+
s_manager.OnChangeFont -= OnChangeFont;
127+
}
128+
}
129+
130+
void OnChangeFont(Font font)
131+
{
132+
TextComponent.font = font;
133+
}
134+
}
135+
}

Runtime/Component/LocalizeTextGroup.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/LocalizeManager.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ public ILocalizeManager AddData(IEnumerable<ILocalizeData> datas)
2626
public ILocalizeManager AddFontData(IEnumerable<ILocalizeFontData> datas)
2727
{
2828
foreach (ILocalizeFontData data in datas)
29-
_fontDictionary.Add(data.GetLanguage(), data);
29+
{
30+
SystemLanguage language = data.GetLanguage();
31+
if (_fontDictionary.TryGetValue(language, out var alreadyExistData))
32+
{
33+
Debug.LogError($"{nameof(LocalizeManager)} {nameof(AddFontData)} already contain font, language:{language}, font:{alreadyExistData.GetFont().name}, other font:{data.GetFont().name}");
34+
continue;
35+
}
36+
_fontDictionary.Add(language, data);
37+
}
3038

3139
return this;
3240
}
@@ -36,8 +44,10 @@ public ILocalizeManager ChangeLanguage(SystemLanguage language)
3644
_currentLanguage = language;
3745
OnChangeLanguage?.Invoke(language);
3846

39-
TryGetFont(out Font font);
40-
OnChangeFont?.Invoke(font);
47+
if (TryGetFont(out Font font))
48+
{
49+
OnChangeFont?.Invoke(font);
50+
}
4151

4252
return this;
4353
}

0 commit comments

Comments
 (0)