public static IEnumerable <GameObject> FindObjectsByPattern ( IEnumerable <string> namePatterns, IEnumerable <string> ignorePatterns, string componentPattern ) { if (namePatterns == null || !namePatterns.Any()) { return(!System.String.IsNullOrEmpty(componentPattern) ? FindObjectsByComponent(componentPattern, ignorePatterns) : new List <GameObject>()); } // The first pattern is required // If there any patterns after the first, then at least one must match var requiredPattern = "*" + namePatterns.First() + "*"; var additionalPatterns = namePatterns.Skip(1) .Select(pattern => "*" + pattern + "*") .ToArray(); System.Func <string, bool> isNameAMatch = (name) => { return(StringUtil.GlobMatch(name, requiredPattern) && (additionalPatterns.Length == 0 || additionalPatterns.Any(pattern => StringUtil.GlobMatch(name, pattern)))); }; var matchingObjects = GameObjectUtil.GetAllGameObjects() .Where(gameObject => isNameAMatch(gameObject.name)); if (ignorePatterns != null) { foreach (var ignorePattern in ignorePatterns) { matchingObjects = RemoveObjectsByPattern(matchingObjects, ignorePattern); } } if (!System.String.IsNullOrEmpty(componentPattern)) { matchingObjects = FindObjectsWithComponentByPattern(matchingObjects, componentPattern); } return(matchingObjects); }
public static IEnumerable <GameObject> FindObjectsByComponent ( string componentPattern, IEnumerable <string> ignorePatterns = null ) { var gameObjects = GameObjectUtil.GetAllGameObjects(); var matchingObjects = FindObjectsWithComponentByPattern(gameObjects, componentPattern); if (ignorePatterns != null) { foreach (var ignorePattern in ignorePatterns) { matchingObjects = RemoveObjectsByPattern(matchingObjects, ignorePattern); } } return(matchingObjects); }