示例#1
0
        /// <summary>
        ///     Destroy all components of the specified type attached to current <see cref="GameObject" />.
        /// </summary>
        /// <param name="self">The <see cref="GameObject" /> instance components attached to.</param>
        /// <typeparam name="T">Type of components to destroy.</typeparam>
        /// <returns>Number of components destroyed.</returns>
        public static int DestroyComponents <T>(this GameObject self)
            where T : Component
        {
            var components = self.GetComponents <T>();

            foreach (var component in components)
            {
                UnityObjectUtil.Destroy(component);
            }
            return(components.Length);
        }
示例#2
0
        public static void DestroyChildren(this Transform self, Action <Transform> beforeDestroy = null)
        {
            Ensure.Argument.NotNull(self, nameof(self));

            while (self.childCount > 0)
            {
                var child = self.GetChild(0);
                beforeDestroy?.Invoke(child);
                child.SetParent(null);
                UnityObjectUtil.Destroy(child.gameObject);
            }
        }
示例#3
0
        public static void DestroyChildren(this Transform self,
                                           Func <Transform, bool> predicate = null, Action <Transform> beforeDestroy = null)
        {
            UnityEnsure.Argument.NotNull(self, nameof(self));

            var destroyList = self.OfType <Transform>().Where(c => predicate == null || predicate(c)).ToArray();

            foreach (var child in destroyList)
            {
                beforeDestroy?.Invoke(child);
                UnityObjectUtil.Destroy(child.gameObject);
            }
        }
示例#4
0
        public static void DestroyChildren(this Transform self, Func <Transform, bool> predicate)
        {
            Ensure.Argument.NotNull(self, nameof(self));

            var destroyList = new List <Transform>();

            for (int i = 0; i < self.childCount; ++i)
            {
                var child = self.GetChild(i);
                if (predicate.InvokeIfNotNull(child))
                {
                    destroyList.Add(child);
                }
            }
            for (int i = 0; i < destroyList.Count; ++i)
            {
                UnityObjectUtil.Destroy(destroyList[i].gameObject);
            }
        }
示例#5
0
 private static void DestroyInstance(RootBehaviour inst)
 {
     UnityObjectUtil.Destroy(inst.gameObject);
     _instances.Remove(inst);
 }