예제 #1
0
        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);
            }
        }
예제 #2
0
        /** 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);
            }
        }
        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();
        }
예제 #4
0
        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();
        }