void OnDestroy() { if (instance == this) { instance = null; } }
public override void OnInspectorGUI() { LanguageSetter t = (LanguageSetter)target; GUIStyle style = new GUIStyle(); style.wordWrap = true; if (EditorApplication.isPlaying) { GUILayout.Label("Current language: " + Languages.Current); if (SupportedLanguages.Instance != null) { foreach (SupportedLanguages.SupportedLanguage lang in SupportedLanguages.Instance.supportedLanguages) { if (GUILayout.Button("Switch to " + lang.language)) { Languages.Current = lang.language; } } } else { GUILayout.Label("You do not have an asset of type SupportedLanguages yet."); if (GUILayout.Button("Create one")) { SupportedLanguages.ShowSupportedLanguagesMenu(); } } } else { GUILayout.Label("You can emulate changing languages from this inspector in play mode.", style); GUILayout.Space(15); GUILayout.Label("Add your prefab to this component, then press Bake, to instantiate your prefab under this Transform once for each supported language your game has. Once instantiated, the GameObject will immediately receive a BakeLanguage(Language) message.", style); GUILayout.Space(5); var prefabProperty = serializedObject.FindProperty("prefab"); EditorGUILayout.PropertyField(prefabProperty, GUIContent.none); serializedObject.ApplyModifiedProperties(); if (t.prefab != null) { if (PrefabUtility.GetPrefabType(t.prefab) == PrefabType.Prefab) { if (GUILayout.Button("Bake")) { t.Bake(); } } else { GUILayout.Label("Not a prefab."); } } EditorUtility.SetDirty(t); } }
void OnEnable() { if (instance == this) { return; //already Enabled. } if (instance != null) { Debug.LogError("You should only have one asset of type SupportedLanguages in your project!"); return; } instance = this; }
/** Opens the inspector for the current Languages asset, or create a new one if there's none in this project. */ public static void ShowSupportedLanguagesMenu() { if (Instance != null) { Selection.activeObject = Instance; //show inspector } else { //Create the new asset: SupportedLanguages languages = ScriptableObject.CreateInstance <SupportedLanguages>(); languages.supportedLanguages = new List <SupportedLanguage>() { new SupportedLanguage(Language.English) }; AssetDatabase.CreateAsset(languages, "Assets/Languages.asset"); Debug.Log("Created Assets/Languages.asset"); Selection.activeObject = languages; //show inspector } }
/** Called when pressing Bake in inspector */ public void Bake() { if (transform.childCount != 0) { if (!EditorUtility.DisplayDialog(name + " is not empty", "Are you sure you want to bake this LanguageSetter? Its children will be deleted.", "ok", "cancel")) { return; } } if (SupportedLanguages.Instance == null) { if (EditorUtility.DisplayDialog("Missing languages", "You do not have an asset of type SupportedLanguages in your project. Create one?", "ok", "cancel")) { SupportedLanguages.ShowSupportedLanguagesMenu(); } return; } while (transform.childCount > 0) { DestroyImmediate(transform.GetChild(0).gameObject); } //duplicated prefab for each supported language foreach (SupportedLanguages.SupportedLanguage lang in SupportedLanguages.Instance.supportedLanguages) { GameObject go = (GameObject)PrefabUtility.InstantiatePrefab(prefab); go.transform.SetParent(transform, false); go.name = prefab.name + " (" + lang.language + ")"; go.SendMessage("BakeLanguage", lang.language, SendMessageOptions.DontRequireReceiver); EditorUtility.SetDirty(go); } EditorUtility.SetDirty(this); EditorSceneManager.MarkAllScenesDirty(); if (EditorUtility.DisplayDialog("Language options provided", "This Language Setter has done its job. It is safe to remove it now. Do you want us to remove it?", "Yes", "I'll keep it for now")) { DestroyImmediate(this); } }
/** Helper function to get language from system locale */ static Language fromSystem() { Language userLanguage = Language.English; switch (Application.systemLanguage) { case SystemLanguage.English: userLanguage = Language.English; break; case SystemLanguage.Afrikaans: userLanguage = Language.Afrikaans; break; case SystemLanguage.Arabic: userLanguage = Language.Arabic; break; case SystemLanguage.Basque: userLanguage = Language.Basque; break; case SystemLanguage.Belarusian: userLanguage = Language.Belarusian; break; case SystemLanguage.Bulgarian: userLanguage = Language.Bulgarian; break; case SystemLanguage.Catalan: userLanguage = Language.Catalan; break; case SystemLanguage.Chinese: case SystemLanguage.ChineseSimplified: userLanguage = Language.ChineseSimplified; break; case SystemLanguage.ChineseTraditional: userLanguage = Language.ChineseTraditional; break; case SystemLanguage.Czech: userLanguage = Language.Czech; break; case SystemLanguage.Danish: userLanguage = Language.Danish; break; case SystemLanguage.Dutch: userLanguage = Language.Dutch; break; case SystemLanguage.Estonian: userLanguage = Language.Estonian; break; case SystemLanguage.Faroese: userLanguage = Language.Faroese; break; case SystemLanguage.Finnish: userLanguage = Language.Finnish; break; case SystemLanguage.French: userLanguage = Language.French; break; case SystemLanguage.German: userLanguage = Language.German; break; case SystemLanguage.Greek: userLanguage = Language.Greek; break; case SystemLanguage.Hebrew: userLanguage = Language.Hebrew; break; case SystemLanguage.Hungarian: userLanguage = Language.Hungarian; break; case SystemLanguage.Icelandic: userLanguage = Language.Icelandic; break; case SystemLanguage.Indonesian: userLanguage = Language.Indonesian; break; case SystemLanguage.Italian: userLanguage = Language.Italian; break; case SystemLanguage.Japanese: userLanguage = Language.Japanese; break; case SystemLanguage.Korean: userLanguage = Language.Korean; break; case SystemLanguage.Latvian: userLanguage = Language.Latvian; break; case SystemLanguage.Lithuanian: userLanguage = Language.Lithuanian; break; case SystemLanguage.Norwegian: userLanguage = Language.Norwegian; break; case SystemLanguage.Polish: userLanguage = Language.Polish; break; case SystemLanguage.Portuguese: userLanguage = Language.Portuguese; break; case SystemLanguage.Romanian: userLanguage = Language.Romanian; break; case SystemLanguage.Russian: userLanguage = Language.Russian; break; case SystemLanguage.SerboCroatian: userLanguage = Language.SerboCroatian; break; case SystemLanguage.Slovak: userLanguage = Language.Slovak; break; case SystemLanguage.Slovenian: userLanguage = Language.Slovenian; break; case SystemLanguage.Spanish: userLanguage = Language.Spanish; break; case SystemLanguage.Swedish: userLanguage = Language.Swedish; break; case SystemLanguage.Thai: userLanguage = Language.Thai; break; case SystemLanguage.Turkish: userLanguage = Language.Turkish; break; case SystemLanguage.Ukrainian: userLanguage = Language.Ukrainian; break; case SystemLanguage.Vietnamese: userLanguage = Language.Vietnamese; break; default: userLanguage = Language.English; break; } //check whether userLanguage is supported by the app if (!SupportedLanguages.IsLanguageSupported(userLanguage)) { //a few different cases here. switch (userLanguage) { //from either form of Chinese, attempt to fall back onto the other form, or English case Language.ChineseSimplified: if (SupportedLanguages.IsLanguageSupported(Language.ChineseTraditional)) { userLanguage = Language.ChineseTraditional; } else { userLanguage = Language.English; } break; case Language.ChineseTraditional: if (SupportedLanguages.IsLanguageSupported(Language.ChineseSimplified)) { userLanguage = Language.ChineseSimplified; } else { userLanguage = Language.English; } break; //from Catalan, attempt to fall back onto Spanish, or English case Language.Catalan: if (SupportedLanguages.IsLanguageSupported(Language.Spanish)) { userLanguage = Language.Spanish; } else { userLanguage = Language.English; } break; //from everything else, attempt to fall back onto English default: userLanguage = Language.English; break; } } //if the language has fallen back onto English but English isn't supported, fall back to the default language if (!SupportedLanguages.IsLanguageSupported(userLanguage)) { userLanguage = SupportedLanguages.DefaultLanguage; } return(userLanguage); }
public override void OnInspectorGUI() { SupportedLanguages t = target as SupportedLanguages; if (t == null) { return; } if (t != SupportedLanguages.Instance) { GUILayout.Label("You should only have one asset of\ntype SupportedLanguages in your project!"); GUILayout.Label("Delete this asset."); return; } bool possibleTranslation = true; //will be set to false if default language is not supported by AutoTranslation //Setting default language: GUILayout.Label("Select your own default language."); GUILayout.Label("This is the language you make your\ninterfaces in Unity in."); GUILayout.Label("For example if you have UI Texts with\ntheir text property in French, select French."); t.defaultLanguage = (Language)EditorGUILayout.EnumPopup(t.defaultLanguage); if (AutoTranslation.TranslationLanguageCode(t.defaultLanguage) == "") { //impossible to translate from this one! GUILayout.Label(t.defaultLanguage + " will not support automatic translations."); possibleTranslation = false; } if (t.defaultLanguage == Language.Default) { t.defaultLanguage = Language.English; } //check that default language is among supported languages: bool contains = false; foreach (SupportedLanguages.SupportedLanguage sl in t.supportedLanguages) { if (sl.language == t.defaultLanguage) { contains = true; break; } } if (!contains) { t.supportedLanguages.Add(new SupportedLanguages.SupportedLanguage(t.defaultLanguage)); } GUILayout.Space(30); //Displaying available languages: GUILayout.Label("Languages your game will be available in:"); foreach (SupportedLanguages.SupportedLanguage lang in t.supportedLanguages) { GUILayout.BeginHorizontal(); GUILayout.Label("- " + lang.language); bool defaultLang = lang.language == t.defaultLanguage; if (defaultLang) { GUIStyle style = new GUIStyle(); style.normal.textColor = new Color(0.5f, 0.5f, 0.5f); GUILayout.Label("(Can't remove default language)", style); } else { if (GUILayout.Button("Remove " + lang.language)) { t.supportedLanguages.Remove(lang); break; } } GUILayout.EndHorizontal(); //Select font: GUILayout.BeginHorizontal(); GUILayout.Label("\tFont for " + lang.language); lang.font = (Font)EditorGUILayout.ObjectField(lang.font, typeof(Font), true); GUILayout.EndHorizontal(); //Select automatic translation: if (!defaultLang && possibleTranslation && AutoTranslation.TranslationLanguageCode(lang.language) != "") { GUILayout.BeginHorizontal(); GUILayout.Label("\tAuto-translate to " + lang.language); lang.translate = GUILayout.Toggle(lang.translate, ""); GUILayout.EndHorizontal(); } else { lang.translate = false; //can't have translations from default to default (or if default can't be translated) } GUILayout.Space(5); } GUILayout.Space(15); //Adding a language: GUILayout.Label("Add a language:"); GUILayout.BeginHorizontal(); selected = (Language)EditorGUILayout.EnumPopup(selected); if (selected == Language.Default) { selected = Language.English; } //check whether it contains it contains = false; foreach (SupportedLanguages.SupportedLanguage sl in t.supportedLanguages) { if (sl.language == selected) { contains = true; break; } } if (contains) { GUILayout.Label(selected + " is already supported."); } else { if (GUILayout.Button("Add " + selected)) { t.supportedLanguages.Add(new SupportedLanguages.SupportedLanguage(selected)); } } GUILayout.EndHorizontal(); GUILayout.Space(20); GUILayout.Label("Note: when you modify this asset, it's a\ngood idea to go through all translated\nelements in your project to make sure\neverything looks and behaves fine."); GUILayout.Space(20); //Sample: if (GUILayout.Button(sample != null ? "Update sample text" : "Show sample text")) { sample = new LanguageSamples.Sample(); foreach (SupportedLanguages.SupportedLanguage lang in t.supportedLanguages) { sample.AddLanguage(lang.language, lang.font); } sample.Prepare(); } if (sample != null) { sampleFont = (Font)EditorGUILayout.ObjectField("Font used for sample", sampleFont, typeof(Font), true); Font display = sampleFont; if (display == null) { display = (Font)Resources.GetBuiltinResource <Font>("Arial.ttf"); } sampleFontSize = (int)EditorGUILayout.Slider("Font size", sampleFontSize, 10, 100); GUILayout.Label("Note: this sample might not contain all\ncharacters used in your game and should\nsimply be used as a first idea."); sample.DisplayResults(display, sampleFontSize); } EditorUtility.SetDirty(t); }
public override void OnInspectorGUI() { serializedObject.Update(); LocalizedSprite elem = target as LocalizedSprite; Undo.RecordObject(elem, "Modified localized sprite"); if (SupportedLanguages.Instance != null) { //display what this LocalizedSprite will translate: string willTranslate = "Will localize "; bool ok = true; if (elem.GetComponent <Image>()) { willTranslate += "UI Image component on"; } else if (elem.GetComponent <SpriteRenderer>()) { willTranslate += "SpriteRenderer component on"; } else { ok = false; //can't do that ya'll! } willTranslate += "\nthis GameObject."; if (!ok) { GUIStyle style = new GUIStyle(); style.wordWrap = true; GUILayout.Label("LocalizedSprites need to be on a GameObject with a UI Image or SpriteRenderer component attached!", style); } GUILayout.Label(willTranslate); GUILayout.Space(10); foreach (SupportedLanguages.SupportedLanguage language in SupportedLanguages.Instance.supportedLanguages) { GUILayout.Label("" + language.language); bool found = false; foreach (LocalizedSprite.Translation t in elem.translations) { if (t.language == language.language) { found = true; if (language.language == SupportedLanguages.Instance.defaultLanguage) { if (!EditorApplication.isPlaying) { t.contents = defaultContents(elem); } if (t.contents == null) { t.contents = (Sprite)EditorGUILayout.ObjectField(t.contents, typeof(Sprite), true); } else { GUILayout.Label("(uses default sprite " + t.contents.name + ")"); } } else { t.contents = (Sprite)EditorGUILayout.ObjectField(t.contents, typeof(Sprite), true); } break; } } if (!found) { LocalizedSprite.Translation t = new LocalizedSprite.Translation(); t.language = language.language; if (language.language == SupportedLanguages.Instance.defaultLanguage) { if (!EditorApplication.isPlaying) { t.contents = defaultContents(elem); } if (t.contents == null) { t.contents = (Sprite)EditorGUILayout.ObjectField(null, typeof(Sprite), true); } else { GUILayout.Label("(uses default sprite " + t.contents.name + ")"); } } else { t.contents = (Sprite)EditorGUILayout.ObjectField((Sprite)null, typeof(Sprite), true); } elem.translations.Add(t); } } //check whether any translations are provided for unsupported languages, and delete them. foreach (LocalizedSprite.Translation t in elem.translations) { bool found = false; foreach (SupportedLanguages.SupportedLanguage lang in SupportedLanguages.Instance.supportedLanguages) { if (lang.language == t.language) { found = true; break; } } if (!found) { elem.translations.Remove(t); Debug.LogWarning("Removed localized sprite " + t.language + " from " + elem.name); break; } } if (GUILayout.Button("Edit supported languages")) { SupportedLanguages.ShowSupportedLanguagesMenu(); } } else { if (GUILayout.Button("Create supported languages")) { SupportedLanguages.ShowSupportedLanguagesMenu(); } } serializedObject.ApplyModifiedProperties(); }
public override void OnInspectorGUI() { serializedObject.Update(); TranslatedElement elem = target as TranslatedElement; Undo.RecordObject(elem, "Modified translation"); if (SupportedLanguages.Instance != null) { //display what this TranslatedElement will translate: string willTranslate = "Will translate "; if (elem.GetComponent <Text>()) { willTranslate += "UI Text component on"; } else if (elem.GetComponent <TextMesh>()) { willTranslate += "Text Mesh component on"; } else { willTranslate += "name of"; } willTranslate += "\nthis GameObject."; GUILayout.Label(willTranslate); GUILayout.Space(10); foreach (SupportedLanguages.SupportedLanguage language in SupportedLanguages.Instance.supportedLanguages) { GUILayout.BeginHorizontal(); GUILayout.Label("" + language.language); if (language.font != null) { GUIStyle style = new GUIStyle(); style.normal.textColor = new Color(0.5f, 0.5f, 0.5f); GUILayout.Label("(uses font " + language.font.name + ")", style); } GUILayout.EndHorizontal(); bool found = false; foreach (TranslatedElement.Translation t in elem.translations) { if (t.language == language.language) { found = true; t.font = language.font; if (language.language == SupportedLanguages.Instance.defaultLanguage) { if (!EditorApplication.isPlaying) { t.contents = defaultContents(elem); } GUILayout.Label(t.contents); } else { t.contents = EditorGUILayout.TextField(t.contents); if (language.translate && t.contents == "") { if (autoTranslation == null && defaultContents(elem) != "") { //let's start auto-translating this! //Attempt to find an active monobehaviour in the scene to handle the coroutine. Transform coroutineElem = elem.transform; while (!coroutineElem.gameObject.activeInHierarchy) { if (coroutineElem.parent == null) { break; } coroutineElem = coroutineElem.parent; } while (coroutineElem.GetComponent <MonoBehaviour>() == null) { //find the first active child with a monobehaviour if (coroutineElem.childCount <= 0) { break; //no luck :'( } foreach (Transform child in coroutineElem) { if (child.gameObject.activeInHierarchy) { if (child.GetComponent <MonoBehaviour>() != null) { coroutineElem = child; break; } } } } autoTranslation = new AutoTranslation(defaultContents(elem), SupportedLanguages.Instance.defaultLanguage, t.language, coroutineElem.GetComponent <MonoBehaviour>()); } else if (autoTranslation.Finished && autoTranslation.TargetLanguage == t.language) { //done translating this language! t.contents = autoTranslation.Translated; autoTranslation = null; } } } break; } } if (!found) { TranslatedElement.Translation t = new TranslatedElement.Translation(); t.language = language.language; if (language.language == SupportedLanguages.Instance.defaultLanguage) { if (!EditorApplication.isPlaying) { t.contents = defaultContents(elem); } GUILayout.Label(t.contents); } else { t.contents = EditorGUILayout.TextField(""); } elem.translations.Add(t); } } //check whether any translations are provided for unsupported languages, and delete them. foreach (TranslatedElement.Translation t in elem.translations) { bool found = false; foreach (SupportedLanguages.SupportedLanguage lang in SupportedLanguages.Instance.supportedLanguages) { if (lang.language == t.language) { found = true; break; } } if (!found) { elem.translations.Remove(t); Debug.LogWarning("Removed translation " + t.language + " from " + elem.name); break; } } if (GUILayout.Button("Edit supported languages")) { SupportedLanguages.ShowSupportedLanguagesMenu(); } } else { if (GUILayout.Button("Create supported languages")) { SupportedLanguages.ShowSupportedLanguagesMenu(); } } serializedObject.ApplyModifiedProperties(); }