Example #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);
        }
Example #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);
            }
        }
Example #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);
            }
        }
Example #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);
            }
        }
Example #5
0
        public static T GetInstance(bool createOnNotFound)
        {
            if (instance != null)
            {
                return(instance);
            }

            instance = UnityObjectUtil.FindUniqueSceneComponent <T>();

            if (instance == null)
            {
                if (createOnNotFound)
                {
                    var gameObject = new GameObject(typeof(T).Name);
                    instance = gameObject.AddComponent <T>();
                }
                else
                {
                    throw new InvalidOperationException($@"Instance of ""{typeof(T)}"" not found.");
                }
            }

            return(instance);
        }
Example #6
0
 private static void DestroyInstance(RootBehaviour inst)
 {
     UnityObjectUtil.Destroy(inst.gameObject);
     _instances.Remove(inst);
 }