Sprite defaultContents(LocalizedSprite elem)
 {
     if (elem.GetComponent <Image>())
     {
         return(elem.GetComponent <Image>().sprite);
     }
     else if (elem.GetComponent <SpriteRenderer>())
     {
         return(elem.GetComponent <SpriteRenderer>().sprite);
     }
     else
     {
         return(null);
     }
 }
        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();
        }