Tanoda
Localization_SOURCE_Editor.cs
Go to the documentation of this file.
1#if UNITY_EDITOR
2using UnityEditor;
3using UnityEngine;
4
5//---Written by Matej Vanco 20.10.2018 dd/mm/yyyy
6//---Updated by Matej Vanco 05.01.2021 dd/mm/yyyy
7//---Language Localization - Editor
8
9[CustomEditor(typeof(Localization_SOURCE))]
10[CanEditMultipleObjects]
11public class Localization_SOURCE_Editor : Editor
12{
13 private SerializedProperty LanguageFiles, SelectedLanguage;
14 private SerializedProperty LoadLanguageOnStart;
15
16 private SerializedProperty LocalizationSelector;
17
18 private SerializedProperty AT_GameObjectChildsRoot;
19 private SerializedProperty ExtraCanvas1;
20 private SerializedProperty ExtraCanvas2;
21 private SerializedProperty ExtraCanvas3;
22
23 private bool DrawList;
24
25 private SerializedProperty quickActions;
26
27 private Localization_SOURCE l;
28 private bool addKey;
29 private bool categorySelected;
30 private int category;
31
32 private string[] categories;
33
34 private void OnEnable()
35 {
36 l = (Localization_SOURCE) target;
37
38 LanguageFiles = serializedObject.FindProperty("LanguageFiles");
39 SelectedLanguage = serializedObject.FindProperty("SelectedLanguage");
40 LoadLanguageOnStart = serializedObject.FindProperty("LoadLanguageOnStart");
41 LocalizationSelector = serializedObject.FindProperty("LocalizationSelector");
42 AT_GameObjectChildsRoot = serializedObject.FindProperty("AT_GameObjectChildsRoot");
43 ExtraCanvas1 = serializedObject.FindProperty("ExtraCanvas1");
44 ExtraCanvas2 = serializedObject.FindProperty("ExtraCanvas2");
45 ExtraCanvas3 = serializedObject.FindProperty("ExtraCanvas3");
46 quickActions = serializedObject.FindProperty("quickActions");
47
48 categories = new string[l.Categories.Count + 1];
49 categories[0] = "All";
50 for (var i = 0; i < l.Categories.Count; i++)
51 categories[i + 1] = l.Categories[i];
52 }
53
54 public override void OnInspectorGUI()
55 {
56 if (target == null) return;
57 serializedObject.Update();
58
59 ps();
60
61 pv();
62 pP(LanguageFiles, "Language Files", "", true);
63 pP(SelectedLanguage, "Selected Language", "Currently selected language for the localization");
64 ps(5);
65 pP(LoadLanguageOnStart, "Load Language On Start", "Load localization after program startup");
66 pve();
67
68 ps(15);
69
70 pv();
71 if (pb("Add Key"))
72 {
73 Localization_SOURCE_Window.Init();
74 addKey = true;
75 l.Internal_RefreshInternalLocalization();
76 }
77
78 if (addKey)
79 {
80 pv();
81 if (pb("X", GUILayout.Width(40)))
82 {
83 addKey = false;
84 categorySelected = false;
85 category = 0;
86 return;
87 }
88
89 ple("From Category:");
90 pv();
91 for (var i = 0; i < l.Categories.Count; i++)
92 if (pb(l.Categories[i]))
93 {
94 categorySelected = true;
95 category = i;
96 return;
97 }
98
99 pve();
100 if (categorySelected)
101 {
102 ple("Key:");
103 if (pb("Add All", GUILayout.Width(120)))
104 {
105 for (var i = 0; i < Localization_SOURCE_Window.localizationElements.Count; i++)
106 {
107 if (Localization_SOURCE_Window.localizationElements[i].Category != category)
108 continue;
109 l.Internal_AddKey(Localization_SOURCE_Window.localizationElements[i].Key);
110 }
111
112 addKey = false;
113 categorySelected = false;
114 category = 0;
115 return;
116 }
117
118 EditorGUILayout.BeginVertical("Box");
119 for (var i = 0; i < Localization_SOURCE_Window.localizationElements.Count; i++)
120 {
121 if (Localization_SOURCE_Window.localizationElements[i].Category != category)
122 continue;
123 var passed = true;
124 foreach (var sel in l.LocalizationSelector)
125 if (sel.Key == Localization_SOURCE_Window.localizationElements[i].Key)
126 {
127 passed = false;
128 break;
129 }
130
131 if (!passed)
132 continue;
133 if (pb(Localization_SOURCE_Window.localizationElements[i].Key))
134 {
135 l.Internal_AddKey(Localization_SOURCE_Window.localizationElements[i].Key);
136 addKey = false;
137 categorySelected = false;
138 category = 0;
139 return;
140 }
141 }
142
143 EditorGUILayout.EndVertical();
144 }
145
146 pve();
147 }
148
149 pve();
150
151 ps(15);
152
153 pv();
154 l.selectedCategory = EditorGUILayout.Popup(new GUIContent("Filter Category: "), l.selectedCategory, categories);
155 pv();
156 pP(quickActions, "Quick Actions", "Edit keys with quick actions", true);
157 if (serializedObject.FindProperty("quickActions").isExpanded)
158 if (pb($"Apply To All Keys in '{categories[l.selectedCategory]}' Category"))
159 foreach (var sel in l.LocalizationSelector)
160 {
161 if (l.selectedCategory != 0 && sel.Category != l.selectedCategory - 1) continue;
162 sel.AssignationType = l.quickActions.assignationType;
164 sel.AT_UITextComponentAllowed = l.quickActions.UITextAllowed;
165 sel.AT_TextMeshComponentAllowed = l.quickActions.TextMeshAllowed;
167 {
168 sel.AT_TextMeshbject = null;
169 sel.AT_UITextObject = null;
170 sel.AT_UIObjects = null;
171 }
172
173 if (l.quickActions.SpecificUITexts.Length > 0)
174 sel.AT_UITextObject = l.quickActions.SpecificUITexts;
175 if (l.quickActions.SpecificTextMeshes.Length > 0)
176 sel.AT_TextMeshbject = l.quickActions.SpecificTextMeshes;
177 }
178
179 pve();
180 ps();
181
182 pv();
183 pP(AT_GameObjectChildsRoot, "GameObject Childs Root",
184 "Starting root for keys containing 'GameObjectChild' assignation type");
185 pP(ExtraCanvas1, "Extra Root 1", "Starting root for keys containing 'AutoFindText' assignation type");
186 pP(ExtraCanvas2, "Extra Root 2", "Starting root for keys containing 'AutoFindText' assignation type");
187 pP(ExtraCanvas3, "Extra Root 3", "Starting root for keys containing 'AutoFindText' assignation type");
188 GUILayout.Toggle(DrawList, "Draw List");
189 if (l.LocalizationSelector.Count > 0 && DrawList) pDrawList();
190 pve();
191 pve();
192 }
193
194 private void pDrawList()
195 {
196 for (var i = 0; i < l.LocalizationSelector.Count; i++)
197 {
198 if (l.selectedCategory != 0 && l.LocalizationSelector[i].Category != l.selectedCategory - 1) continue;
199
200 pv();
201 var item = LocalizationSelector.GetArrayElementAtIndex(i);
202 GUILayout.BeginHorizontal();
203
204 pP(item, l.LocalizationSelector[i].Key);
205 if (pb("X", GUILayout.Width(40)))
206 {
207 l.LocalizationSelector.RemoveAt(i);
208 return;
209 }
210
211 GUILayout.EndHorizontal();
212 if (!item.isExpanded)
213 {
214 pve();
215 continue;
216 }
217
218 var sec = l.LocalizationSelector[i];
219
220 ps(5);
221
222 EditorGUI.indentLevel += 1;
223 GUILayout.BeginHorizontal("Box");
224 ple("Key: " + sec.Key, true);
225 pl("Category: " + l.Categories[sec.Category]);
226 GUILayout.EndHorizontal();
227
228 ps();
229
230 pP(item.FindPropertyRelative("AssignationType"), "Assignation Type");
231
232 switch (sec.AssignationType)
233 {
235 pv();
236 pP(item.FindPropertyRelative("AT_FindChildByKeyName"), "Find Child By Key Name",
237 "If enabled, the system will find the child of the selected component type [below] by the key name");
238 if (!sec.AT_FindChildByKeyName)
239 pP(item.FindPropertyRelative("AT_ChildName"), "Child Name");
240 else
241 EditorGUILayout.HelpBox("Object with name '" + sec.Key + "' should exist", MessageType.None);
242 pve();
243 ps(3);
244
245 pv();
246 pP(item.FindPropertyRelative("AT_UseGeneralChildsRootObject"), "Use General Childs Root Object");
247 if (!sec.AT_UseGeneralChildsRootObject)
248 pP(item.FindPropertyRelative("AT_CustomChildsRootObject"), "Custom Childs Root Object");
249 pve();
250
251 ps(3);
252
253 pv();
254 pP(item.FindPropertyRelative("AT_UITextComponentAllowed"), "UIText Component Allowed",
255 "If disabled, objects with UI Text component will be ignored");
256 pP(item.FindPropertyRelative("AT_TextMeshComponentAllowed"), "TextMesh Component Allowed",
257 "If disabled, objects with Text Mesh component will be ignored");
258 pve();
259 break;
260
262 pv();
263 EditorGUILayout.HelpBox(
264 "Object with Localization_KEY component should have an ID '" + sec.Key + "'", MessageType.None);
265 pve();
266 ps(3);
267
268 pv();
269 pP(item.FindPropertyRelative("AT_UITextComponentAllowed"), "UIText Component Allowed",
270 "If disabled, objects with UI Text component will be ignored");
271 pP(item.FindPropertyRelative("AT_TextMeshComponentAllowed"), "TextMesh Component Allowed",
272 "If disabled, objects with Text Mesh component will be ignored");
273 pve();
274 break;
275
277 pP(item.FindPropertyRelative("AT_UITextObject"), "Specific UI Text",
278 "Assign specific UI Text objects", true);
279 break;
280
282 pP(item.FindPropertyRelative("AT_UIObjects"), "Text Replacer", "Assign specific UI objects", true);
283 break;
284
286 pP(item.FindPropertyRelative("AT_TextMeshbject"), "Specific Text Mesh",
287 "Assign specific Text Mesh objects", true);
288 break;
289 }
290
291
292 EditorGUI.indentLevel -= 1;
293 pve();
294 }
295 }
296
297 #region LayoutShortcuts
298
299 private void pl(string text)
300 {
301 GUILayout.Label(text);
302 }
303
304 private void ple(string text, bool bold = false)
305 {
306 if (bold)
307 {
308 var add = "<b>";
309 add += text + "</b>";
310 text = add;
311 }
312
313 var style = new GUIStyle();
314 style.richText = true;
315 style.normal.textColor = Color.white;
316 EditorGUILayout.LabelField(text, style);
317 }
318
319 private void ps(float space = 10)
320 {
321 GUILayout.Space(space);
322 }
323
324 private void pP(SerializedProperty p, string Text, string ToolTip = "", bool includeChilds = false)
325 {
326 EditorGUILayout.PropertyField(p, new GUIContent(Text, ToolTip), includeChilds);
327 serializedObject.ApplyModifiedProperties();
328 }
329
330 private void pv()
331 {
332 GUILayout.BeginVertical("Box");
333 }
334
335 private void pve()
336 {
337 GUILayout.EndVertical();
338 }
339
340 private bool pb(string mess, GUILayoutOption opt = null)
341 {
342 if (opt == null) return GUILayout.Button(mess);
343 return GUILayout.Button(mess, opt);
344 }
345
346 #endregion
347}
348#endif
UnityEngine.Color Color
Definition: TestScript.cs:32
_LocalizationSelector._AssignationType assignationType
List< string > Categories
List< _LocalizationSelector > LocalizationSelector