private void ShowList(SerializedProperty list) { EditorGUI.indentLevel++; // remove the keywords already assigned from the registered list SpeechInputHandler handler = target as SpeechInputHandler; string[] availableKeywords = registeredKeywords.Except(handler.keywords.Select(keywordAndResponse => keywordAndResponse.Keyword)).ToArray(); // keyword rows for (int index = 0; index < list.arraySize; index++) { // the element SerializedProperty elementProperty = list.GetArrayElementAtIndex(index); EditorGUILayout.BeginHorizontal(); bool elementExpanded = EditorGUILayout.PropertyField(elementProperty); GUILayout.FlexibleSpace(); // the remove element button bool elementRemoved = GUILayout.Button(removeButtonContent, EditorStyles.miniButton, miniButtonWidth); if (elementRemoved) { list.DeleteArrayElementAtIndex(index); } EditorGUILayout.EndHorizontal(); if (!elementRemoved && elementExpanded) { SerializedProperty keywordProperty = elementProperty.FindPropertyRelative("Keyword"); string[] keywords = availableKeywords.Concat(new[] { keywordProperty.stringValue }).OrderBy(keyword => keyword).ToArray(); int previousSelection = ArrayUtility.IndexOf(keywords, keywordProperty.stringValue); int currentSelection = EditorGUILayout.Popup("Keyword", previousSelection, keywords); if (currentSelection != previousSelection) { keywordProperty.stringValue = keywords[currentSelection]; } SerializedProperty responseProperty = elementProperty.FindPropertyRelative("Response"); EditorGUILayout.PropertyField(responseProperty, true); } } // add button row EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); // the add element button if (GUILayout.Button(addButtonContent, EditorStyles.miniButton, miniButtonWidth)) { list.InsertArrayElementAtIndex(list.arraySize); } EditorGUILayout.EndHorizontal(); EditorGUI.indentLevel--; }
public override void OnInspectorGUI() { serializedObject.Update(); ShowList(keywordsProperty); serializedObject.ApplyModifiedProperties(); // error and warning messages if (keywordsProperty.arraySize == 0) { EditorGUILayout.HelpBox("No keywords have been assigned!", MessageType.Warning); } else { SpeechInputHandler handler = target as SpeechInputHandler; string duplicateKeyword = handler.keywords.GroupBy(keyword => keyword.Keyword.ToLower()).Where(group => group.Count() > 1).Select(group => group.Key).FirstOrDefault(); if (duplicateKeyword != null) { EditorGUILayout.HelpBox("Keyword '" + duplicateKeyword + "' is assigned more than once!", MessageType.Warning); } } }