Ejemplo n.º 1
0
        private void OnEnable()
        {
            m_AvailableLanguages       = serializedObject.FindProperty("m_AvailableLanguages2");
            m_ImportLocation           = serializedObject.FindProperty("m_ImportLocation");
            m_GoogleAuthenticationFile = serializedObject.FindProperty("GoogleAuthenticationFile");
            if (m_AvailableLanguages != null)
            {
                m_AvailableLanguagesList = new ReorderableList
                                           (
                    serializedObject: serializedObject,
                    elements: m_AvailableLanguages,
                    draggable: true,
                    displayHeader: true,
                    displayAddButton: true,
                    displayRemoveButton: true
                                           );
                m_AvailableLanguagesList.drawHeaderCallback = (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, "Available Languages");
                };
                m_AvailableLanguagesList.drawElementCallback = (rect, index, isActive, isFocused) =>
                {
                    var languageProperty = m_AvailableLanguagesList.serializedProperty.GetArrayElementAtIndex(index);
                    var position         = new Rect(rect.x, rect.y + 2, rect.width, EditorGUIUtility.singleLineHeight);

                    var isCustom = languageProperty.FindLanguageCustomProperty().boolValue;
                    if (isCustom)
                    {
                        var languageName = languageProperty.FindLanguageNameProperty();
                        var languageCode = languageProperty.FindLanguageCodeProperty();

                        var labelWidth = EditorGUIUtility.labelWidth;

                        EditorGUIUtility.labelWidth = 40;
                        var r1 = new Rect(position.x, position.y, position.width / 2 - 2, position.height);
                        EditorGUI.PropertyField(r1, languageName, new GUIContent(languageName.displayName, "Language name"));

                        EditorGUIUtility.labelWidth = 40;
                        var r2 = new Rect(position.x + r1.width + 4, position.y, position.width / 2 - 2, position.height);
                        EditorGUI.PropertyField(r2, languageCode, new GUIContent(languageCode.displayName, "ISO-639-1 code"));

                        EditorGUIUtility.labelWidth = labelWidth;
                    }
                    else
                    {
                        LanguageEditorUtility.LanguageField(position, languageProperty, GUIContent.none, true);
                    }
                };
                m_AvailableLanguagesList.onRemoveCallback = list =>
                {
                    var languageProperty = list.serializedProperty.GetArrayElementAtIndex(list.index);
                    var language         = LanguageEditorUtility.GetLanguageValueFromProperty(languageProperty);
                    if (language.Custom)
                    {
                        var localizedAssets = Localization.FindAllLocalizedAssets();
                        if (localizedAssets.Any(x =>
                                                x.LocaleItems.Any(y => y.Language == language)))
                        {
                            if (!EditorUtility.DisplayDialog("Remove \"" + language + "\" language?",
                                                             "\"" + language + "\" language is in-use by some localized assets." +
                                                             " Are you sure to remove?", "Remove", "Cancel"))
                            {
                                return; // Cancelled.
                            }
                        }
                    }

                    ReorderableList.defaultBehaviours.DoRemoveButton(list);
                };
                m_AvailableLanguagesList.onCanRemoveCallback   = list => list.count > 1;
                m_AvailableLanguagesList.onAddDropdownCallback = (buttonRect, list) =>
                {
                    var menu = new GenericMenu();
                    menu.AddItem(new GUIContent("Language", "Adds built-in language."), false, () =>
                    {
                        ReorderableList.defaultBehaviours.DoAddButton(list);

                        var languageProperty = list.serializedProperty.GetArrayElementAtIndex(list.index);
                        LanguageEditorUtility.SetLanguageProperty(languageProperty, Language.BuiltinLanguages[0]);

                        serializedObject.ApplyModifiedProperties();
                    });
                    menu.AddItem(new GUIContent("Custom language", "Adds custom language."), false, () =>
                    {
                        ReorderableList.defaultBehaviours.DoAddButton(list);

                        var languageProperty = list.serializedProperty.GetArrayElementAtIndex(list.index);
                        LanguageEditorUtility.SetLanguageProperty(languageProperty, "", "", true);

                        serializedObject.ApplyModifiedProperties();
                    });
                    menu.AddItem(new GUIContent("Adds languages in-use", "Adds by searching used languages in assets."), false, () =>
                    {
                        AddUsedLocales();
                        serializedObject.ApplyModifiedProperties();
                    });
                    menu.ShowAsContext();
                };
            }
        }
Ejemplo n.º 2
0
 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
 {
     LanguageEditorUtility.LanguageField(position, property, label);
 }