HKI Core
LocalizationSystemEditor.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
3 using System.Collections.Generic;
4 using HKI.Core.Editor.Misc;
5 
6 namespace HKI.Core.Loc.Editor
7 {
11  [CustomEditor(typeof(LocalizationSystem))]
12  public class LocalizationSystemEditor : UnityEditor.Editor
13  {
14  // Private variables
15  const float buttonHeight = 50.0f;
16 
17  const string textLocalizationTemplateSPName = "TextLocalizationTemplate";
18  const string audioLocalizationTemplateSPName = "AudioLocalizationTemplate";
19 
20  const string textLocalizationElementsSPName = "TextLocalizationElements";
21  const string audioLocalizationElementsSPName = "AudioLocalizationElements";
22 
23  const string textLocalizationContainerSPName = "TextLocalizationContainer";
24  const string audioLocalizationContainerSPName = "AudioLocalizationContainer";
25 
26  SerializedProperty textLocalizationContainerSP = null;
27  SerializedProperty audioLocalizationContainerSP = null;
28 
29  SerializedProperty textLocalizationTemplateSP = null;
30  SerializedProperty audioLocalizationTemplateSP = null;
31 
32  static bool showLocalizationTemplates = false;
33 
34 
35  static string newLocalizationName = "";
36  static bool creationIsPossible = false;
37 
38  static TextLocalization newTextLocalization = null;
39  static SerializedObject newTextLocalizationSO = null;
40  static SerializedProperty newTextLocalizationSP = null;
41 
42  static AudioLocalization newAudioLocalization = null;
43  static SerializedObject newAudioLocalizationSO = null;
44  static SerializedProperty newAudioLocalizationSP = null;
45 
46 
47  static int selectedSearchModeId = 0;
48  static readonly string[] searchModes = { "Text", "Audio" };
49  static string searchName = "";
50 
51  List<SerializedProperty> searchResults = new List<SerializedProperty>();
52 
53  static Vector2 scrollVector = Vector2.zero;
54 
55  static int selectedTypeId = 0;
56  static string selectedName = "";
57  static string selectedChangeName = "";
58  static SerializedObject selectedSO = null;
59  static SerializedProperty selectedSP = null;
60 
61  static bool renameSelected = false;
62  static bool deleteSelected = false;
63 
64  [MenuItem("HKI/Core/Subsystems/Localization System")]
66  {
67  CreateLocalizationSystem();
68  }
69 
74  public static void CreateLocalizationSystem(string rootFolder = null)
75  {
76  if(EditorHelper.FindAndSelectIfExists("Localization System"))
77  return;
78 
79  // Create a new localization system
80  string folder = null;
81 
82  if (!string.IsNullOrEmpty(rootFolder))
83  folder = rootFolder;
84 
85  if (string.IsNullOrEmpty(folder))
86  {
87  folder = EditorHelper.GetFolderFromUser("Select folder for localization system to be saved in", "The given path for the localization system is null or empty");
88  if (string.IsNullOrEmpty(folder))
89  return;
90  }
91 
92  EditorUtility.DisplayProgressBar("Create localization system", "Create localization system", 0.0f);
93  LocalizationSystem localizationSystem = EditorHelper.CreateAsset<LocalizationSystem>("Localization System", folder);
94 
95  EditorUtility.DisplayProgressBar("Create localization system", "Create localization system", 0.4f);
96  SerializedObject localizationSystemSO = new SerializedObject(localizationSystem);
97  localizationSystemSO.Update();
98 
99  EditorUtility.DisplayProgressBar("Create localization system", "Create text localization template", 0.5f);
100  TextLocalization textLocalizationTemplate = ScriptableObject.CreateInstance<TextLocalization>();
101  textLocalizationTemplate.name = "Text Localization Template";
102  AssetDatabase.AddObjectToAsset(textLocalizationTemplate, localizationSystem);
103  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(textLocalizationTemplate));
104  SerializedProperty textLocalizationElementTemplateSP = localizationSystemSO.FindProperty(textLocalizationTemplateSPName);
105  textLocalizationElementTemplateSP.objectReferenceValue = textLocalizationTemplate;
106 
107  EditorUtility.DisplayProgressBar("Create localization system", "Create audio localization template", 0.8f);
108  AudioLocalization audioLocalizationTemplate = ScriptableObject.CreateInstance<AudioLocalization>();
109  audioLocalizationTemplate.name = "Audio Localization Template";
110  AssetDatabase.AddObjectToAsset(audioLocalizationTemplate, localizationSystem);
111  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(audioLocalizationTemplate));
112  SerializedProperty audioLocalizationElementTemplateSP = localizationSystemSO.FindProperty(audioLocalizationTemplateSPName);
113  audioLocalizationElementTemplateSP.objectReferenceValue = audioLocalizationTemplate;
114 
115  localizationSystemSO.ApplyModifiedProperties();
116 
117  EditorUtility.DisplayProgressBar("Create localization system", "Finished", 1.0f);
118 
119  // Finish
121 
122  Selection.activeObject = localizationSystem;
123 
124  EditorUtility.ClearProgressBar();
125  }
126 
127  // OnEnable
128  void OnEnable()
129  {
130  textLocalizationTemplateSP = serializedObject.FindProperty(textLocalizationTemplateSPName);
131  audioLocalizationTemplateSP = serializedObject.FindProperty(audioLocalizationTemplateSPName);
132 
133  textLocalizationContainerSP = serializedObject.FindProperty(textLocalizationContainerSPName);
134  audioLocalizationContainerSP = serializedObject.FindProperty(audioLocalizationContainerSPName);
135 
136  Search();
137  }
138 
139  // OnInspectorGUI function
140  public override void OnInspectorGUI()
141  {
142  serializedObject.Update();
143 
144  EditorGUILayout.LabelField("Localization templates", EditorStyles.boldLabel);
145 
146  showLocalizationTemplates = EditorGUILayout.Foldout(showLocalizationTemplates, "Localization templates");
147 
148  if(showLocalizationTemplates)
149  {
150  EditorGUILayout.LabelField("Text localization template");
151 
152  if (textLocalizationTemplateSP.objectReferenceValue != null)
153  {
154  SerializedObject textLocalizationTemplateSO = new SerializedObject(textLocalizationTemplateSP.objectReferenceValue);
155  TextLocalizationEditor.Draw(textLocalizationTemplateSO, textLocalizationTemplateSO.FindProperty(textLocalizationElementsSPName));
156  }
157 
158  EditorGUILayout.Space();
159 
160  EditorGUILayout.LabelField("Audio localization template");
161 
162  if (textLocalizationTemplateSP.objectReferenceValue != null)
163  {
164  SerializedObject audioLocalizationTemplateSO = new SerializedObject(audioLocalizationTemplateSP.objectReferenceValue);
165  AudioLocalizationEditor.Draw(audioLocalizationTemplateSO, audioLocalizationTemplateSO.FindProperty(audioLocalizationElementsSPName));
166  }
167  }
168 
169  EditorGUILayout.Space();
170 
171  EditorGUILayout.LabelField("Add new localization", EditorStyles.boldLabel);
172 
173  EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
174 
175  EditorGUI.BeginChangeCheck();
176  newLocalizationName = EditorGUILayout.TextField(newLocalizationName);
177  if(EditorGUI.EndChangeCheck())
178  {
179  creationIsPossible = !string.IsNullOrEmpty(newLocalizationName);
180  }
181 
182  GUI.enabled = creationIsPossible;
183  if (GUILayout.Button("New Text Localization", EditorStyles.toolbarButton, GUILayout.Width(130.0f)))
184  {
185  newTextLocalization = (TextLocalization)Object.Instantiate(textLocalizationTemplateSP.objectReferenceValue);
186  newTextLocalization.name = newLocalizationName;
187  AssetDatabase.AddObjectToAsset(newTextLocalization, target);
188  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newTextLocalization));
189 
190  textLocalizationContainerSP.InsertArrayElementAtIndex(textLocalizationContainerSP.arraySize);
191  textLocalizationContainerSP.GetArrayElementAtIndex(textLocalizationContainerSP.arraySize - 1).objectReferenceValue = newTextLocalization;
192 
193  newTextLocalizationSO = new SerializedObject(newTextLocalization);
194  newTextLocalizationSP = newTextLocalizationSO.FindProperty(textLocalizationElementsSPName);
195 
196  newAudioLocalization = null;
197  newAudioLocalizationSO = null;
198  newAudioLocalizationSP = null;
199  creationIsPossible = false;
200 
201  serializedObject.ApplyModifiedProperties();
202  serializedObject.Update();
203 
204  Search();
205  }
206 
207  if (GUILayout.Button("New Audio Localization", EditorStyles.toolbarButton, GUILayout.Width(140.0f)))
208  {
209  newAudioLocalization = (AudioLocalization)Object.Instantiate(audioLocalizationTemplateSP.objectReferenceValue);
210  newAudioLocalization.name = newLocalizationName;
211  AssetDatabase.AddObjectToAsset(newAudioLocalization, target);
212  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newAudioLocalization));
213 
214  audioLocalizationContainerSP.InsertArrayElementAtIndex(audioLocalizationContainerSP.arraySize);
215  audioLocalizationContainerSP.GetArrayElementAtIndex(audioLocalizationContainerSP.arraySize - 1).objectReferenceValue = newAudioLocalization;
216 
217  newAudioLocalizationSO = new SerializedObject(newAudioLocalization);
218  newAudioLocalizationSP = newAudioLocalizationSO.FindProperty(audioLocalizationElementsSPName);
219 
220  newTextLocalization = null;
221  newTextLocalizationSO = null;
222  newTextLocalizationSP = null;
223  creationIsPossible = false;
224 
225  serializedObject.ApplyModifiedProperties();
226  serializedObject.Update();
227 
228  Search();
229  }
230  GUI.enabled = true;
231 
232  EditorGUILayout.EndHorizontal();
233 
234  if(newTextLocalization != null && newTextLocalizationSO != null && newTextLocalizationSP != null)
235  TextLocalizationEditor.Draw(newTextLocalizationSO, newTextLocalizationSP);
236  else if(newAudioLocalization != null && newAudioLocalizationSO != null && newAudioLocalizationSP != null)
237  AudioLocalizationEditor.Draw(newAudioLocalizationSO, newAudioLocalizationSP);
238  else
239  EditorGUILayout.LabelField("No new localization");
240 
241  EditorGUILayout.Space();
242 
243  EditorGUILayout.LabelField("Search for localization", EditorStyles.boldLabel);
244  EditorGUI.BeginChangeCheck();
245  EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
246 
247  selectedSearchModeId = EditorGUILayout.Popup(selectedSearchModeId, searchModes, EditorStyles.toolbarPopup, GUILayout.Width(60.0f));
248 
249  searchName = EditorGUILayout.TextField("", searchName, "ToolbarSeachTextField");
250  if(GUILayout.Button("", "ToolbarSeachCancelButton"))
251  searchName = "";
252 
253  EditorGUILayout.EndHorizontal();
254  if(EditorGUI.EndChangeCheck())
255  {
256  Search();
257  }
258 
259 
260  EditorGUILayout.BeginVertical(EditorStyles.helpBox);
261  EditorGUILayout.BeginScrollView(scrollVector, GUILayout.Height(300.0f));
262 
263  if(searchResults.Count == 0)
264  {
265  EditorGUILayout.LabelField("No search results");
266  }
267  else
268  {
269  foreach(SerializedProperty property in searchResults)
270  {
271  if (GUILayout.Button(property.objectReferenceValue.name, EditorStyles.toolbarButton))
272  {
273  selectedSO = new SerializedObject(property.objectReferenceValue);
274  selectedSP = selectedSO.FindProperty(selectedSearchModeId == 0 ? textLocalizationElementsSPName : audioLocalizationElementsSPName);
275  selectedTypeId = selectedSearchModeId;
276  selectedName = property.objectReferenceValue.name;
277  selectedChangeName = property.objectReferenceValue.name;
278  }
279  }
280  }
281 
282  EditorGUILayout.EndScrollView();
283  EditorGUILayout.EndVertical();
284 
285  EditorGUILayout.Space();
286 
287  EditorGUILayout.LabelField("Selected", EditorStyles.boldLabel);
288  if(selectedTypeId == 0 && selectedSO != null && selectedSP != null)
289  {
290  ChangeNameOfSelected();
291 
292  EditorGUILayout.Space();
293 
294  TextLocalizationEditor.Draw(selectedSO, selectedSP);
295  }
296  else if(selectedTypeId == 1 && selectedSO != null && selectedSP != null)
297  {
298  ChangeNameOfSelected();
299 
300  EditorGUILayout.Space();
301 
302  AudioLocalizationEditor.Draw(selectedSO, selectedSP);
303  }
304  else
305  EditorGUILayout.LabelField("Nothing selected");
306 
307  if(renameSelected)
308  {
309  if((newTextLocalization != null && newTextLocalization.name == selectedName) || (newAudioLocalization != null && newAudioLocalization.name == selectedName))
310  creationIsPossible = true;
311 
312  AssetDatabase.ClearLabels(selectedSO.targetObject);
313  selectedSO.targetObject.name = selectedChangeName;
314  AssetDatabase.SetLabels(selectedSO.targetObject, new string[] { selectedChangeName });
315  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(selectedSO.targetObject));
316 
317  selectedName = selectedChangeName;
318 
319  Search();
320  renameSelected = false;
321  }
322 
323  if(deleteSelected)
324  {
325  if((newTextLocalization != null && newTextLocalization.name == selectedName) || (newAudioLocalization != null && newAudioLocalization.name == selectedName))
326  creationIsPossible = true;
327 
328  int deleteIndex = -1;
329 
330  SerializedProperty container = selectedTypeId == 0 ? textLocalizationContainerSP : audioLocalizationContainerSP;
331 
332  for (int i = 0; i < container.arraySize; i++)
333  {
334  if(container.GetArrayElementAtIndex(i).objectReferenceValue == selectedSO.targetObject)
335  {
336  deleteIndex = i;
337  break;
338  }
339  }
340 
341  if(deleteIndex != -1)
342  {
343  if(container.GetArrayElementAtIndex(deleteIndex) != null)
344  container.DeleteArrayElementAtIndex(deleteIndex);
345 
346  container.DeleteArrayElementAtIndex(deleteIndex);
347  UnityEngine.Object.DestroyImmediate(selectedSO.targetObject, true);
348  AssetDatabase.SaveAssets();
349  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
350  }
351 
352  selectedName = "";
353  selectedChangeName = "";
354  selectedSO = null;
355  selectedSP = null;
356 
357  Search();
358 
359  deleteSelected = false;
360  }
361 
362  serializedObject.ApplyModifiedProperties();
363  }
364 
365  void Search()
366  {
367  searchResults.Clear();
368 
369  if(!string.IsNullOrEmpty(searchName))
370  {
371  SerializedProperty container = selectedSearchModeId == 0 ? textLocalizationContainerSP : audioLocalizationContainerSP;
372 
373  for (int i = 0; i < container.arraySize; i++)
374  {
375  SerializedProperty searchSP = container.GetArrayElementAtIndex(i);
376 
377  if (searchSP.objectReferenceValue.name.Contains(searchName))
378  searchResults.Add(searchSP);
379  }
380  }
381  }
382 
384  {
385  EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
386 
387  selectedChangeName = EditorGUILayout.TextField(selectedChangeName);
388 
389  GUI.enabled = selectedName != selectedChangeName && !string.IsNullOrEmpty(selectedChangeName);
390 
391  if(GUILayout.Button("Change name", EditorStyles.toolbarButton, GUILayout.Width(100.0f)))
392  renameSelected = true;
393 
394  GUI.enabled = true;
395 
396  if (GUILayout.Button("Delete", EditorStyles.toolbarButton, GUILayout.Width(80.0f)))
397  deleteSelected = true;
398 
399  EditorGUILayout.EndHorizontal();
400  }
401  }
402 }
static void AddScriptableObjectToInitializer(ScriptableObject obj)
This function adds a scriptable object as a subasset to the initializer asset.
This ScriptableObject contains as a container for all text localization information.
static bool FindAndSelectIfExists(string filter, string fileEnding=".asset")
Definition: EditorHelper.cs:13
static void CreateLocalizationSystem(string rootFolder=null)
This functio creates a localization system.
This class containes helper functions for editor scripts.
Definition: EditorHelper.cs:10
This ScriptableObject holds the data for all localization. Most of its functionality is implemented b...
static string GetFolderFromUser(string panelText, string errorMsg)
Definition: EditorHelper.cs:32
static void Draw(SerializedObject serializedObject, SerializedProperty property)
Custom inspector for the AudioLocalization class.
This class contains the creation function of the menu for all HKI Core systems.
static void Draw(SerializedObject serializedObject, SerializedProperty property)
Custom inspector for the LocalizationSystem class.
Custom inspector for the TextLocalization class.
This ScriptableObject contains as a container for all audio localization information.