Exemplo n.º 1
0
        public static IEnumerable <Component> GetComponentsInOnlyChildren(this Transform target, Type type,
                                                                          SearchStrategy strategy =
                                                                          SearchStrategy.FindComponent)
        {
            var queue = new Queue <Transform>();

            AddChildren(target);

            while (queue.Any())
            {
                var transform  = queue.Dequeue();
                var components = strategy.FindComponents(transform.gameObject, type);

                foreach (var component in components)
                {
                    yield return(component);
                }

                AddChildren(transform);
            }

            void AddChildren(Transform transform)
            {
                for (var index = 0; index < transform.childCount; index++)
                {
                    var child = transform.GetChild(index);
                    queue.Enqueue(child);
                }
            }
        }
Exemplo n.º 2
0
        private static void ResolveCollection(Component behaviour, object obj, FieldInfo field, SearchStrategy strategy)
        {
            if (field.FieldType.IsGenericType == false)
            {
                var message = $"{typeof(FindComponentAttribute)} found on non-generic collection field: {field}. " +
                              "This is not currently supported.";
                Debug.LogWarning(message);
                return;
            }

            var itemTypes = field.FieldType.GetGenericArguments();

            if (itemTypes.Length > 1)
            {
                var message = $"{typeof(FindComponentAttribute)} found on collection field with multiple type arguments: {field}  " +
                              "This is not currently supported.";
                Debug.LogWarning(message);
                return;
            }

            var itemType = itemTypes.First();

            var fieldValue = field.GetValue(obj);

            if (fieldValue == null)
            {
                return;
            }

            var list = (IList)fieldValue;

            var items    = strategy.FindComponents(behaviour.gameObject, itemType);
            var newItems = items.Except(list.Contains);

            foreach (var item in newItems.ToArray())
            {
                list.Add(item);
            }
        }