3 using System.Collections.Generic;
6 namespace HKI.Core.Loc.Editor
11 [CustomEditor(typeof(LocalizationSystem))]
15 const float buttonHeight = 50.0f;
17 const string textLocalizationTemplateSPName =
"TextLocalizationTemplate";
18 const string audioLocalizationTemplateSPName =
"AudioLocalizationTemplate";
20 const string textLocalizationElementsSPName =
"TextLocalizationElements";
21 const string audioLocalizationElementsSPName =
"AudioLocalizationElements";
23 const string textLocalizationContainerSPName =
"TextLocalizationContainer";
24 const string audioLocalizationContainerSPName =
"AudioLocalizationContainer";
26 SerializedProperty textLocalizationContainerSP = null;
27 SerializedProperty audioLocalizationContainerSP = null;
29 SerializedProperty textLocalizationTemplateSP = null;
30 SerializedProperty audioLocalizationTemplateSP = null;
32 static bool showLocalizationTemplates =
false;
35 static string newLocalizationName =
"";
36 static bool creationIsPossible =
false;
39 static SerializedObject newTextLocalizationSO = null;
40 static SerializedProperty newTextLocalizationSP = null;
43 static SerializedObject newAudioLocalizationSO = null;
44 static SerializedProperty newAudioLocalizationSP = null;
47 static int selectedSearchModeId = 0;
48 static readonly
string[] searchModes = {
"Text",
"Audio" };
49 static string searchName =
"";
51 List<SerializedProperty> searchResults =
new List<SerializedProperty>();
53 static Vector2 scrollVector = Vector2.zero;
55 static int selectedTypeId = 0;
56 static string selectedName =
"";
57 static string selectedChangeName =
"";
58 static SerializedObject selectedSO = null;
59 static SerializedProperty selectedSP = null;
61 static bool renameSelected =
false;
62 static bool deleteSelected =
false;
64 [MenuItem(
"HKI/Core/Subsystems/Localization System")]
67 CreateLocalizationSystem();
82 if (!
string.IsNullOrEmpty(rootFolder))
85 if (
string.IsNullOrEmpty(folder))
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))
92 EditorUtility.DisplayProgressBar(
"Create localization system",
"Create localization system", 0.0f);
95 EditorUtility.DisplayProgressBar(
"Create localization system",
"Create localization system", 0.4f);
96 SerializedObject localizationSystemSO =
new SerializedObject(localizationSystem);
97 localizationSystemSO.Update();
99 EditorUtility.DisplayProgressBar(
"Create localization system",
"Create text localization template", 0.5f);
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;
107 EditorUtility.DisplayProgressBar(
"Create localization system",
"Create audio localization template", 0.8f);
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;
115 localizationSystemSO.ApplyModifiedProperties();
117 EditorUtility.DisplayProgressBar(
"Create localization system",
"Finished", 1.0f);
122 Selection.activeObject = localizationSystem;
124 EditorUtility.ClearProgressBar();
130 textLocalizationTemplateSP = serializedObject.FindProperty(textLocalizationTemplateSPName);
131 audioLocalizationTemplateSP = serializedObject.FindProperty(audioLocalizationTemplateSPName);
133 textLocalizationContainerSP = serializedObject.FindProperty(textLocalizationContainerSPName);
134 audioLocalizationContainerSP = serializedObject.FindProperty(audioLocalizationContainerSPName);
142 serializedObject.Update();
144 EditorGUILayout.LabelField(
"Localization templates", EditorStyles.boldLabel);
146 showLocalizationTemplates = EditorGUILayout.Foldout(showLocalizationTemplates,
"Localization templates");
148 if(showLocalizationTemplates)
150 EditorGUILayout.LabelField(
"Text localization template");
152 if (textLocalizationTemplateSP.objectReferenceValue != null)
154 SerializedObject textLocalizationTemplateSO =
new SerializedObject(textLocalizationTemplateSP.objectReferenceValue);
155 TextLocalizationEditor.
Draw(textLocalizationTemplateSO, textLocalizationTemplateSO.FindProperty(textLocalizationElementsSPName));
158 EditorGUILayout.Space();
160 EditorGUILayout.LabelField(
"Audio localization template");
162 if (textLocalizationTemplateSP.objectReferenceValue != null)
164 SerializedObject audioLocalizationTemplateSO =
new SerializedObject(audioLocalizationTemplateSP.objectReferenceValue);
165 AudioLocalizationEditor.
Draw(audioLocalizationTemplateSO, audioLocalizationTemplateSO.FindProperty(audioLocalizationElementsSPName));
169 EditorGUILayout.Space();
171 EditorGUILayout.LabelField(
"Add new localization", EditorStyles.boldLabel);
173 EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
175 EditorGUI.BeginChangeCheck();
176 newLocalizationName = EditorGUILayout.TextField(newLocalizationName);
177 if(EditorGUI.EndChangeCheck())
179 creationIsPossible = !
string.IsNullOrEmpty(newLocalizationName);
182 GUI.enabled = creationIsPossible;
183 if (GUILayout.Button(
"New Text Localization", EditorStyles.toolbarButton, GUILayout.Width(130.0f)))
185 newTextLocalization = (
TextLocalization)Object.Instantiate(textLocalizationTemplateSP.objectReferenceValue);
186 newTextLocalization.name = newLocalizationName;
187 AssetDatabase.AddObjectToAsset(newTextLocalization, target);
188 AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newTextLocalization));
190 textLocalizationContainerSP.InsertArrayElementAtIndex(textLocalizationContainerSP.arraySize);
191 textLocalizationContainerSP.GetArrayElementAtIndex(textLocalizationContainerSP.arraySize - 1).objectReferenceValue = newTextLocalization;
193 newTextLocalizationSO =
new SerializedObject(newTextLocalization);
194 newTextLocalizationSP = newTextLocalizationSO.FindProperty(textLocalizationElementsSPName);
196 newAudioLocalization = null;
197 newAudioLocalizationSO = null;
198 newAudioLocalizationSP = null;
199 creationIsPossible =
false;
201 serializedObject.ApplyModifiedProperties();
202 serializedObject.Update();
207 if (GUILayout.Button(
"New Audio Localization", EditorStyles.toolbarButton, GUILayout.Width(140.0f)))
209 newAudioLocalization = (
AudioLocalization)Object.Instantiate(audioLocalizationTemplateSP.objectReferenceValue);
210 newAudioLocalization.name = newLocalizationName;
211 AssetDatabase.AddObjectToAsset(newAudioLocalization, target);
212 AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newAudioLocalization));
214 audioLocalizationContainerSP.InsertArrayElementAtIndex(audioLocalizationContainerSP.arraySize);
215 audioLocalizationContainerSP.GetArrayElementAtIndex(audioLocalizationContainerSP.arraySize - 1).objectReferenceValue = newAudioLocalization;
217 newAudioLocalizationSO =
new SerializedObject(newAudioLocalization);
218 newAudioLocalizationSP = newAudioLocalizationSO.FindProperty(audioLocalizationElementsSPName);
220 newTextLocalization = null;
221 newTextLocalizationSO = null;
222 newTextLocalizationSP = null;
223 creationIsPossible =
false;
225 serializedObject.ApplyModifiedProperties();
226 serializedObject.Update();
232 EditorGUILayout.EndHorizontal();
234 if(newTextLocalization != null && newTextLocalizationSO != null && newTextLocalizationSP != null)
236 else if(newAudioLocalization != null && newAudioLocalizationSO != null && newAudioLocalizationSP != null)
239 EditorGUILayout.LabelField(
"No new localization");
241 EditorGUILayout.Space();
243 EditorGUILayout.LabelField(
"Search for localization", EditorStyles.boldLabel);
244 EditorGUI.BeginChangeCheck();
245 EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
247 selectedSearchModeId = EditorGUILayout.Popup(selectedSearchModeId, searchModes, EditorStyles.toolbarPopup, GUILayout.Width(60.0f));
249 searchName = EditorGUILayout.TextField(
"", searchName,
"ToolbarSeachTextField");
250 if(GUILayout.Button(
"",
"ToolbarSeachCancelButton"))
253 EditorGUILayout.EndHorizontal();
254 if(EditorGUI.EndChangeCheck())
260 EditorGUILayout.BeginVertical(EditorStyles.helpBox);
261 EditorGUILayout.BeginScrollView(scrollVector, GUILayout.Height(300.0f));
263 if(searchResults.Count == 0)
265 EditorGUILayout.LabelField(
"No search results");
269 foreach(SerializedProperty property
in searchResults)
271 if (GUILayout.Button(property.objectReferenceValue.name, EditorStyles.toolbarButton))
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;
282 EditorGUILayout.EndScrollView();
283 EditorGUILayout.EndVertical();
285 EditorGUILayout.Space();
287 EditorGUILayout.LabelField(
"Selected", EditorStyles.boldLabel);
288 if(selectedTypeId == 0 && selectedSO != null && selectedSP != null)
290 ChangeNameOfSelected();
292 EditorGUILayout.Space();
296 else if(selectedTypeId == 1 && selectedSO != null && selectedSP != null)
298 ChangeNameOfSelected();
300 EditorGUILayout.Space();
305 EditorGUILayout.LabelField(
"Nothing selected");
309 if((newTextLocalization != null && newTextLocalization.name == selectedName) || (newAudioLocalization != null && newAudioLocalization.name == selectedName))
310 creationIsPossible =
true;
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));
317 selectedName = selectedChangeName;
320 renameSelected =
false;
325 if((newTextLocalization != null && newTextLocalization.name == selectedName) || (newAudioLocalization != null && newAudioLocalization.name == selectedName))
326 creationIsPossible =
true;
328 int deleteIndex = -1;
330 SerializedProperty container = selectedTypeId == 0 ? textLocalizationContainerSP : audioLocalizationContainerSP;
332 for (
int i = 0; i < container.arraySize; i++)
334 if(container.GetArrayElementAtIndex(i).objectReferenceValue == selectedSO.targetObject)
341 if(deleteIndex != -1)
343 if(container.GetArrayElementAtIndex(deleteIndex) != null)
344 container.DeleteArrayElementAtIndex(deleteIndex);
346 container.DeleteArrayElementAtIndex(deleteIndex);
347 UnityEngine.Object.DestroyImmediate(selectedSO.targetObject,
true);
348 AssetDatabase.SaveAssets();
349 AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
353 selectedChangeName =
"";
359 deleteSelected =
false;
362 serializedObject.ApplyModifiedProperties();
367 searchResults.Clear();
369 if(!
string.IsNullOrEmpty(searchName))
371 SerializedProperty container = selectedSearchModeId == 0 ? textLocalizationContainerSP : audioLocalizationContainerSP;
373 for (
int i = 0; i < container.arraySize; i++)
375 SerializedProperty searchSP = container.GetArrayElementAtIndex(i);
377 if (searchSP.objectReferenceValue.name.Contains(searchName))
378 searchResults.Add(searchSP);
385 EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
387 selectedChangeName = EditorGUILayout.TextField(selectedChangeName);
389 GUI.enabled = selectedName != selectedChangeName && !
string.IsNullOrEmpty(selectedChangeName);
391 if(GUILayout.Button(
"Change name", EditorStyles.toolbarButton, GUILayout.Width(100.0f)))
392 renameSelected =
true;
396 if (GUILayout.Button(
"Delete", EditorStyles.toolbarButton, GUILayout.Width(80.0f)))
397 deleteSelected =
true;
399 EditorGUILayout.EndHorizontal();
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")
static void CreateLocalizationSystem(string rootFolder=null)
This functio creates a localization system.
This class containes helper functions for editor scripts.
This ScriptableObject holds the data for all localization. Most of its functionality is implemented b...
static string GetFolderFromUser(string panelText, string errorMsg)
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.
static void CreateLocalizationSystemMenu()
void ChangeNameOfSelected()
override void OnInspectorGUI()
This ScriptableObject contains as a container for all audio localization information.