暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InputActionAssetSearchProvider.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 InputActionAssetSearchProviders
  10. {
  11. const string k_AssetFolderSearchProviderId = "AssetsInputActionAssetSearchProvider";
  12. const string k_ProjectWideActionsSearchProviderId = "ProjectWideInputActionAssetSearchProvider";
  13. const string k_ProjectWideAssetIdentificationString = " [Project Wide Input Actions]";
  14. internal static SearchProvider CreateInputActionAssetSearchProvider()
  15. {
  16. return CreateInputActionAssetSearchProvider(k_AssetFolderSearchProviderId,
  17. "Asset Input Action Assets",
  18. (obj) => { return obj != null ? AssetDatabase.GetAssetPath(obj) : "Null"; },
  19. () => LoadInputActionAssetsFromAssetDatabase(skipProjectWide: true));
  20. }
  21. internal static SearchProvider CreateInputActionAssetSearchProviderForProjectWideActions()
  22. {
  23. return CreateInputActionAssetSearchProvider(k_ProjectWideActionsSearchProviderId,
  24. "Project-Wide Input Action Asset",
  25. (obj) => { return obj != null ? AssetDatabase.GetAssetPath(obj) : "Null"; },
  26. () => LoadInputActionReferencesFromAsset());
  27. }
  28. private static IEnumerable<Object> LoadInputActionReferencesFromAsset()
  29. {
  30. var asset = InputSystem.actions;
  31. if (asset == null)
  32. return Array.Empty<Object>();
  33. return new List<InputActionAsset>() { asset };
  34. }
  35. private static IEnumerable<Object> LoadInputActionAssetsFromAssetDatabase(bool skipProjectWide)
  36. {
  37. string[] searchFolders = new string[] { "Assets" };
  38. var inputActionAssetGUIDs = AssetDatabase.FindAssets($"t:{typeof(InputActionAsset).Name}", searchFolders);
  39. var inputActionAssetList = new List<InputActionAsset>();
  40. foreach (var guid in inputActionAssetGUIDs)
  41. {
  42. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  43. var assetInputActionAsset = AssetDatabase.LoadAssetAtPath<InputActionAsset>(assetPath);
  44. if (skipProjectWide)
  45. {
  46. if (assetInputActionAsset == InputSystem.actions)
  47. continue;
  48. }
  49. inputActionAssetList.Add(assetInputActionAsset);
  50. }
  51. return inputActionAssetList;
  52. }
  53. private static SearchProvider CreateInputActionAssetSearchProvider(string id, string displayName,
  54. Func<Object, string> createItemFetchDescription, Func<IEnumerable<Object>> fetchAssets)
  55. {
  56. // We assign description+label in FilteredSearch but also provide a fetchDescription+fetchLabel below.
  57. // This is needed to support all zoom-modes for an unknown reason.
  58. // Also, fetchLabel/fetchDescription and what is provided to CreateItem is playing different
  59. // roles at different zoom levels.
  60. var inputActionAssetIcon = InputActionAssetIconLoader.LoadAssetIcon();
  61. return new SearchProvider(id, displayName)
  62. {
  63. priority = 25,
  64. fetchDescription = FetchLabel,
  65. fetchItems = (context, items, provider) => FilteredSearch(context, provider, FetchLabel, createItemFetchDescription,
  66. fetchAssets),
  67. fetchLabel = FetchLabel,
  68. fetchPreview = (item, context, size, options) => inputActionAssetIcon,
  69. fetchThumbnail = (item, context) => inputActionAssetIcon,
  70. toObject = ToObject,
  71. };
  72. }
  73. private static Object ToObject(SearchItem item, Type type)
  74. {
  75. return item.data as Object;
  76. }
  77. // Custom search function with label matching filtering.
  78. private static IEnumerable<SearchItem> FilteredSearch(SearchContext context, SearchProvider provider,
  79. Func<Object, string> fetchObjectLabel, Func<Object, string> createItemFetchDescription, Func<IEnumerable<Object>> fetchAssets)
  80. {
  81. foreach (var asset in fetchAssets())
  82. {
  83. var label = fetchObjectLabel(asset);
  84. Texture2D thumbnail = null; // filled in later
  85. if (!label.Contains(context.searchText, System.StringComparison.InvariantCultureIgnoreCase))
  86. continue; // Ignore due to filtering
  87. yield return provider.CreateItem(context, asset.GetInstanceID().ToString(), label, createItemFetchDescription(asset),
  88. thumbnail, asset);
  89. }
  90. }
  91. // Note that this is overloaded to allow utilizing FetchLabel inside fetchItems to keep label formatting
  92. // consistent between CreateItem and additional fetchLabel calls.
  93. private static string FetchLabel(Object obj)
  94. {
  95. // if (obj == InputSystem.actions) return $"{obj.name}{k_ProjectWideAssetIdentificationString}";
  96. return obj.name;
  97. }
  98. private static string FetchLabel(SearchItem item, SearchContext context)
  99. {
  100. return FetchLabel((item.data as Object) !);
  101. }
  102. }
  103. }
  104. #endif