예제 #1
0
        /// <summary>
        /// 遍历list,并删除符合条件的元素们
        /// </summary>
        /// <param name="list"></param>
        /// <param name="match"></param>
        /// <typeparam name="T">返回true则删除,否则跳过</typeparam>
        /// <returns></returns>
        public static int RemoveInList <T>(List <T> list, VeePredicate <T> match)
        {
            int removeCount = 0;

            if (list == null)
            {
                return(removeCount);
            }
            int eleCount = list.Count;

            for (var i = eleCount - 1; i >= 0; --i)
            {
                if (match(list[i]))
                {
                    list.RemoveAt(i);
                    removeCount += 1;
                }
            }

            return(removeCount);
        }
예제 #2
0
        /// <summary>
        /// 查找符合条件的子物体
        /// </summary>
        /// <param name="root">root transform</param>
        /// <param name="match">条件</param>
        /// <param name="recursive">是否递归向下层查找</param>
        /// <returns></returns>
        public static List <Transform> FindChildren(this Transform root, VeePredicate <Transform> match, bool recursive = false)
        {
            var result     = new List <Transform>();
            var childCount = root.childCount;

            for (var i = 0; i < childCount; ++i)
            {
                var child = root.GetChild(i);
                if (match(child))
                {
                    result.Add(child);
                }

                if (recursive)
                {
                    var childMatches = FindChildren(child, match, true);
                    result.AddRange(childMatches.ToArray());
                }
            }

            return(result);
        }
예제 #3
0
        public static int RemoveChild(this Transform root, VeePredicate <Transform> match)
        {
            int removeCount = 0;

            if (root == null)
            {
                return(removeCount);
            }
            int childCnt = root.childCount;

            for (var i = childCnt - 1; i >= 0; --i)
            {
                var child = root.GetChild(i);
                if (match(child))
                {
                    var go = child.gameObject;
                    DestroyGameObject(ref go, true);
                    removeCount += 1;
                }
            }

            return(removeCount);
        }