public static void TypeSearchField(this SerializedProperty _objectProperty, Type[] _dataBase, Action <SerializedProperty, Type> _onSelection) { var buttonStyle = new GUIStyle(EditorStyles.miniButton); if (_objectProperty.objectReferenceValue == null) { EditorGUILayout.BeginHorizontal(); //label var labelWidth = GUI.skin.label.CalcSize(new GUIContent(_objectProperty.displayName)).x; EditorGUILayout.LabelField(_objectProperty.displayName, GUILayout.Width(labelWidth)); //get matches before text field so we can cancel it on enter pressed var matches = FindMatches(_dataBase, input.stringValue); if (_objectProperty.isExpanded) { matches = _dataBase; } //cancel text field on enter if (matches.Length == 1) { if (NGNEditorExtensions.GetKeyDown(KeyCode.Return)) { _onSelection.Invoke(_objectProperty, matches[0]); } } //input field var style = new GUIStyle(EditorStyles.toolbarSearchField); input.stringValue = EditorGUILayout.TextField(input.stringValue, style); if (input.stringValue != null && input.stringValue != "") { input.stringValue = Regex.Replace(input.stringValue, @"\s+", ""); //remove spaces in input _objectProperty.isExpanded = true; } //dropdown button if (GUILayout.Button("▲/▼", buttonStyle, GUILayout.Width(30))) { input.stringValue = ""; _objectProperty.isExpanded = !_objectProperty.isExpanded; } EditorGUILayout.EndHorizontal(); var dropStyle = new GUIStyle(EditorStyles.miniButton); //only show input matches if (_objectProperty.isExpanded) { for (int i = 0; i < matches.Length; i++) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(labelWidth); if (GUILayout.Button(matches[i].Name, dropStyle)) { _onSelection.Invoke(_objectProperty, matches[i]); } EditorGUILayout.EndHorizontal(); } } } }
public static void ObjectSearchField(this SerializedProperty _objectProperty, SerializedProperty _objToSearch, System.Type _typeFilter) { var buttonStyle = new GUIStyle(EditorStyles.miniButton); inputs.TryGetValue(_objectProperty, out StringWrapper input); if (input == null) { inputs.Add(_objectProperty, new StringWrapper()); return; } if (_objectProperty.objectReferenceValue == null) { //get all nested objects on object var props = SearchObjectForObjectProperties(_objToSearch, _typeFilter); EditorGUILayout.BeginHorizontal(); //label var labelWidth = GUI.skin.label.CalcSize(new GUIContent(_objectProperty.displayName)).x; EditorGUILayout.LabelField(_objectProperty.displayName, GUILayout.Width(labelWidth)); //get matches before text field so we can cancel it on enter pressed var matches = FindMatches(props, input.stringValue); //cancel text field on enter if (matches.Length == 1) { if (NGNEditorExtensions.GetKeyDown(KeyCode.Return)) { _objectProperty.objectReferenceValue = matches[0]; } } //input field var style = new GUIStyle(EditorStyles.toolbarSearchField); input.stringValue = EditorGUILayout.TextField(input.stringValue, style); if (input.stringValue != null && input.stringValue != "") { input.stringValue = Regex.Replace(input.stringValue, @"\s+", ""); //remove spaces in input _objectProperty.isExpanded = false; } //dropdown button if (GUILayout.Button("▲/▼", buttonStyle, GUILayout.Width(30))) { input.stringValue = ""; _objectProperty.isExpanded = !_objectProperty.isExpanded; } EditorGUILayout.EndHorizontal(); var dropStyle = new GUIStyle(EditorStyles.miniButton); //show all objects in dropdown if (_objectProperty.isExpanded) { for (int i = 0; i < props.Length; i++) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(labelWidth); if (GUILayout.Button(props[i].name, dropStyle)) { _objectProperty.objectReferenceValue = props[i]; } EditorGUILayout.EndHorizontal(); } } else { //only show input matches for (int i = 0; i < matches.Length; i++) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(labelWidth); if (GUILayout.Button(matches[i].name, dropStyle)) { _objectProperty.objectReferenceValue = matches[i]; } EditorGUILayout.EndHorizontal(); } } } else { ObjectPropertyField(_objectProperty); } }