Ejemplo n.º 1
0
        public static void RemoveComponent([DisallowNull] Component comp)
        {
            var dependencies = RemoveComponentUtils.ComponentDependencies(comp);

            if (!RemoveComponent(comp, dependencies))
            {
                //preserve built-in behavior
                if (RemoveComponentUtils.CanRemoveComponent(comp, dependencies))
                {
                    Undo.DestroyObjectImmediate(comp);
                }
            }
        }
Ejemplo n.º 2
0
        public static bool RemoveComponent([DisallowNull] Component component, IEnumerable <Component> dependencies)
        {
            var additionalDatas = dependencies
                                  .Where(c => c != component && c is IAdditionalData)
                                  .ToList();

            if (!RemoveComponentUtils.CanRemoveComponent(component, dependencies.Where(c => !additionalDatas.Contains(c))))
            {
                return(false);
            }

            bool removed        = true;
            var  isAssetEditing = EditorUtility.IsPersistent(component);

            try
            {
                if (isAssetEditing)
                {
                    AssetDatabase.StartAssetEditing();
                }
                Undo.SetCurrentGroupName($"Remove {component.GetType()} and additional data components");

                // The components with RequireComponent(typeof(T)) also contain the AdditionalData attribute, proceed with the remove
                foreach (var additionalDataComponent in additionalDatas)
                {
                    if (additionalDataComponent != null)
                    {
                        Undo.DestroyObjectImmediate(additionalDataComponent);
                    }
                }
                Undo.DestroyObjectImmediate(component);
            }
            catch
            {
                removed = false;
            }
            finally
            {
                if (isAssetEditing)
                {
                    AssetDatabase.StopAssetEditing();
                }
            }

            return(removed);
        }