找回密码
 立即注册
查看: 308|回复: 0

【Unity】AssetDatabase.FindAssets使用内置表达式

[复制链接]
发表于 2022-1-6 08:40 | 显示全部楼层 |阅读模式
google上找了一圈,没有找到这样的问题,看到的回答对我都没有啥帮助,就自己写了一个!
这是个小问题,就不废话了!
先看看AssetDatabase.FindAssets的api
public static string[] FindAssets(string filter, string[] searchInFolders)
{
  SearchFilter searchFilter = new SearchFilter()
  {
    searchArea = SearchFilter.SearchArea.AllAssets
  };
  SearchUtility.ParseSearchString(filter, searchFilter);
  if (searchInFolders != null && (uint) searchInFolders.Length > 0U)
  {
    searchFilter.folders = searchInFolders;
    searchFilter.searchArea = SearchFilter.SearchArea.SelectedFolders;
  }
  return AssetDatabase.FindAssets(searchFilter);
}
这里能看到FindAssets实际上是通过SearchUtility.ParseSearchString去生成filter的。
internal static bool ParseSearchString(string searchText, SearchFilter filter)
{
  if (string.IsNullOrEmpty(searchText))
    return false;
  filter.ClearSearch();
  filter.originalText = searchText;
  string searchString1 = string.Copy(searchText);
  // 这里是将": "替换成":"
  SearchUtility.RemoveUnwantedWhitespaces(ref searchString1);
  bool flag = false;
  int startIndex1 = SearchUtility.FindFirstPositionNotOf(searchString1, " \t,*?");
  if (startIndex1 == -1)
    startIndex1 = 0;
  int num1;
  for (; startIndex1 < searchString1.Length; startIndex1 = num1 + 1)
  {
    num1 = searchString1.IndexOfAny(" \t,*?".ToCharArray(), startIndex1);
    if (num1 == -1)
      num1 = searchString1.Length;
    int num2 = searchString1.IndexOf('"', startIndex1);
    int startIndex2 = -1;
    if (num2 != -1 && num2 < num1)
    {
      startIndex2 = searchString1.IndexOf('"', num2 + 1);
      if (startIndex2 != -1 && startIndex2 > num1 - 1)
      {
        num1 = searchString1.IndexOfAny(" \t,*?".ToCharArray(), startIndex2);
        if (num1 == -1)
          num1 = searchString1.Length;
      }
    }
    if (num1 > startIndex1)
    {
      string searchString2 = searchString1.Substring(startIndex1, num1 - startIndex1);
      if (SearchUtility.CheckForKeyWords(searchString2, filter, num2 - startIndex1, startIndex2 - startIndex1))
      {
        flag = true;
      }
      else
      {
        SearchFilter searchFilter = filter;
        searchFilter.nameFilter = searchFilter.nameFilter + (string.IsNullOrEmpty(filter.nameFilter) ? "" : " ") + searchString2;
      }
    }
  }
  return flag;
}
大致代码如上,官方文档说明是这样的 https://docs.unity3d.com/2021.1/Documentation/Manual/search-filters.html
所以如果想要在AssetDatabase.FindAssets中使用正则,就需要按照官方文档这样子说明使用才可以。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-22 23:26 , Processed in 0.089224 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表