public override VisualElement CreatePropertyGUI(SerializedProperty property) { var root = Resources.GetTemplate(nameof(LocalesProvider)); var list = new ReorderableList(new List <Locale>()); list.HeaderTitle = "Available Locales"; list.ReorderCallback = ChangeLocaleOrder; list.CreateItemCallback = CreateItem; list.RefreshListCallback = UpdateList; list.RemoveCallback = RemoveSelectedLocale; list.DisplayAddButton = false; // There is no UI Toolkit ObjectPicker in 2019.4 root.Q("locales-list").Add(list); var openGenerator = root.Q <Button>("open-generator-button"); openGenerator.clicked += LocaleGeneratorWindow.ShowWindow; var addCustomLocale = root.Q <Button>("add-custom-locale"); addCustomLocale.clicked += CustomLocaleUIWindow.ShowWindow; var addAll = root.Q <Button>("add-all-button"); addAll.clicked += () => { var assets = AssetDatabase.FindAssets("t:Locale"); for (int i = 0; i < assets.Length; ++i) { LocalizationEditorSettings.AddLocale(AssetDatabase.LoadAssetAtPath <Locale>(AssetDatabase.GUIDToAssetPath(assets[i])), true); } list.RefreshList(); }; Action <Locale> projectLocaleAddedRemoved = l => list.RefreshList(); EventHandler <Locale> projectLocaleSortChanged = (a, b) => list.RefreshList(); root.RegisterCallback <AttachToPanelEvent>(evt => { LocalizationEditorSettings.EditorEvents.LocaleSortOrderChanged += projectLocaleSortChanged; LocalizationEditorSettings.EditorEvents.LocaleAdded += projectLocaleAddedRemoved; LocalizationEditorSettings.EditorEvents.LocaleRemoved += projectLocaleAddedRemoved; }); root.RegisterCallback <DetachFromPanelEvent>(evt => { LocalizationEditorSettings.EditorEvents.LocaleSortOrderChanged -= projectLocaleSortChanged; LocalizationEditorSettings.EditorEvents.LocaleAdded -= projectLocaleAddedRemoved; LocalizationEditorSettings.EditorEvents.LocaleRemoved -= projectLocaleAddedRemoved; }); return(root); }
static void RemoveItem(ReorderableList list, int index) { list.ListProperty.DeleteArrayElementAtIndex(list.Selected); list.ListProperty.serializedObject.ApplyModifiedProperties(); // The bindings will have changed list.RefreshList(); }
static void AddItem(ReorderableList list, int index) { var element = list.ListProperty.InsertArrayElement(index); var name = element.FindPropertyRelative("name"); name.stringValue = list.ListProperty.arraySize > 1 ? $"global-{list.ListProperty.arraySize - 1}" : "global"; element.serializedObject.ApplyModifiedProperties(); list.RefreshList(); }
void AddManagedItem(ReorderableList list, Type type, int index) { var instance = Activator.CreateInstance(type); var element = list.ListProperty.InsertArrayElement(index); var variable = element.FindPropertyRelative("variable"); variable.managedReferenceValue = instance; var name = element.FindPropertyRelative("name"); name.stringValue = list.ListProperty.arraySize > 1 ? $"variable-{list.ListProperty.arraySize - 1}" : "variable"; list.ListProperty.serializedObject.ApplyModifiedProperties(); list.RefreshList(); }
static void ShowAddItemMenu(ReorderableList list, int index) { var menu = new GenericMenu(); var hashSet = new HashSet <string>(); for (int i = 0; i < list.ListProperty.arraySize; ++i) { var element = list.ListProperty.GetArrayElementAtIndex(i); var codeProperty = element.FindPropertyRelative("localeIdentifier.m_Code"); hashSet.Add(codeProperty.stringValue); } var locales = LocalizationEditorSettings.GetLocales(); foreach (var locale in locales) { if (!hashSet.Contains(locale.Identifier.Code)) { menu.AddItem(new GUIContent(locale.ToString()), false, () => { var element = list.ListProperty.InsertArrayElement(index); var codeProperty = element.FindPropertyRelative("localeIdentifier.m_Code"); codeProperty.stringValue = locale.Identifier.Code; list.ListProperty.serializedObject.ApplyModifiedProperties(); list.RefreshList(); }); } else { menu.AddDisabledItem(new GUIContent(locale.ToString()), true); } } menu.ShowAsContext(); }