説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

InputActionReferenceSearchProviders.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEditor.Search;
  6. using UnityEngine.Search;
  7. namespace UnityEngine.InputSystem.Editor
  8. {
  9. internal static class SearchConstants
  10. {
  11. // SearchFlags: these flags are used to customize how search is performed and how search
  12. // results are displayed in the advanced object picker.
  13. // Note: SearchFlags.Packages is not currently used and hides all results from packages.
  14. internal static readonly SearchFlags PickerSearchFlags = SearchFlags.Sorted | SearchFlags.OpenPicker;
  15. // Search.SearchViewFlags : these flags are used to customize the appearance of the PickerWindow.
  16. internal static readonly Search.SearchViewFlags PickerViewFlags = SearchViewFlags.DisableBuilderModeToggle
  17. | SearchViewFlags.DisableInspectorPreview
  18. | SearchViewFlags.ListView
  19. | SearchViewFlags.DisableSavedSearchQuery;
  20. }
  21. internal static class InputActionReferenceSearchProviders
  22. {
  23. const string k_AssetFolderSearchProviderId = "AssetsInputActionReferenceSearchProvider";
  24. const string k_ProjectWideActionsSearchProviderId = "ProjectWideInputActionReferenceSearchProvider";
  25. // Search provider for InputActionReferences for all assets in the project, without project-wide actions.
  26. internal static SearchProvider CreateInputActionReferenceSearchProviderForAssets()
  27. {
  28. return CreateInputActionReferenceSearchProvider(k_AssetFolderSearchProviderId,
  29. "Asset Input Actions",
  30. // Show the asset path in the description.
  31. (obj) => AssetDatabase.GetAssetPath((obj as InputActionReference).asset),
  32. () => InputActionImporter.LoadInputActionReferencesFromAssetDatabase(skipProjectWide: true));
  33. }
  34. // Search provider for InputActionReferences for project-wide actions
  35. internal static SearchProvider CreateInputActionReferenceSearchProviderForProjectWideActions()
  36. {
  37. return CreateInputActionReferenceSearchProvider(k_ProjectWideActionsSearchProviderId,
  38. "Project-Wide Input Actions",
  39. (obj) => "(Project-Wide Input Actions)",
  40. () =>
  41. {
  42. var asset = InputSystem.actions;
  43. if (asset == null)
  44. return Array.Empty<Object>();
  45. var assetPath = AssetDatabase.GetAssetPath(asset);
  46. return InputActionImporter.LoadInputActionReferencesFromAsset(assetPath);
  47. });
  48. }
  49. private static SearchProvider CreateInputActionReferenceSearchProvider(string id, string displayName,
  50. Func<Object, string> createItemFetchDescription, Func<IEnumerable<Object>> fetchAssets)
  51. {
  52. // Match icon used for sub-assets from importer for InputActionReferences.
  53. // We assign description+label in FilteredSearch but also provide a fetchDescription+fetchLabel below.
  54. // This is needed to support all zoom-modes for an unknown reason.
  55. // Also, fetchLabel/fetchDescription and what is provided to CreateItem is playing different
  56. // roles at different zoom levels.
  57. var inputActionReferenceIcon = InputActionAssetIconLoader.LoadActionIcon();
  58. return new SearchProvider(id, displayName)
  59. {
  60. priority = 25,
  61. fetchDescription = FetchLabel,
  62. fetchItems = (context, items, provider) => FilteredSearch(context, provider, FetchLabel, createItemFetchDescription,
  63. fetchAssets, "(Project-Wide Input Actions)"),
  64. fetchLabel = FetchLabel,
  65. fetchPreview = (item, context, size, options) => inputActionReferenceIcon,
  66. fetchThumbnail = (item, context) => inputActionReferenceIcon,
  67. toObject = (item, type) => item.data as Object,
  68. };
  69. }
  70. // Custom search function with label matching filtering.
  71. private static IEnumerable<SearchItem> FilteredSearch(SearchContext context, SearchProvider provider,
  72. Func<Object, string> fetchObjectLabel, Func<Object, string> createItemFetchDescription, Func<IEnumerable<Object>> fetchAssets, string description)
  73. {
  74. foreach (var asset in fetchAssets())
  75. {
  76. var label = fetchObjectLabel(asset);
  77. if (!label.Contains(context.searchText, System.StringComparison.InvariantCultureIgnoreCase))
  78. continue; // Ignore due to filtering
  79. yield return provider.CreateItem(context, asset.GetInstanceID().ToString(), label, createItemFetchDescription(asset),
  80. null, asset);
  81. }
  82. }
  83. // Note that this is overloaded to allow utilizing FetchLabel inside fetchItems to keep label formatting
  84. // consistent between CreateItem and additional fetchLabel calls.
  85. private static string FetchLabel(Object obj)
  86. {
  87. return obj.name;
  88. }
  89. private static string FetchLabel(SearchItem item, SearchContext context)
  90. {
  91. return FetchLabel((item.data as Object) !);
  92. }
  93. }
  94. }
  95. #endif