Example #1
0
        internal static void DestroyObjectHighLevel(Object o, bool forceDestroy = false)
        {
            if (!o)
            {
                return;
            }
            {
                if (o is Component)
                {
                    if (o is MonoBehaviour && (o as MonoBehaviour).IsDestroying())
                    {
                        Debug.LogError("Destroying object multiple times. Don't use DestroyImmediate on the same object in OnDisable or OnDestroy.");
                        return;
                    }
                    Component  c          = o as Component;
                    GameObject gameObject = c.gameObject;
                    if (gameObject)
                    {
                        if (gameObject.IsDestroying())
                        {
                            Debug.LogError("Destroying object multiple times. Don't use DestroyImmediate on the same object in OnDisable or OnDestroy.");
                            return;
                        }

                        if (gameObject.IsActivating())
                        {
                            Debug.LogError("Cannot destroy Component while GameObject is being activated or deactivated.", gameObject);
                            return;
                        }

                        /*
                         * string error;
                         * if (!forceDestroy && !CanRemoveComponent(component, &error))
                         * {
                         *      ErrorStringObject (error, &component);
                         *      return;
                         * }
                         *
                         * // We need to perform an explicit check here for RectTransform because the above 'CanRemoveComponent' allows this type
                         * // to be removed because this action under the editor replaces the RectTransform with a standard Transform.
                         * // Via script, this doesn't happen currently.
                         * if (component.GetClassID () == ClassID(RectTransform))
                         * {
                         *      const char* message = "Can't destroy RectTransform component of '%s'. "
                         *              "If you want to destroy the game object, please call 'Destroy' on the game object instead. "
                         *              "Destroying the RectTransform component is not allowed.";
                         *
                         *      error = Format (message, gameObject->GetName ());
                         *      ErrorStringObject (error, &component);
                         *      return;
                         * }
                         */

                        if (gameObject.IsActive())
                        {
                            c.Deactivate();
                        }

                        c.WillDestroyComponent();
                        gameObject.cs.Remove(c);
                    }
                    else
                    {
                        c.WillDestroyComponent();
                    }
                    c.destroyed = true;
                }
                else if (o is GameObject)
                {
                    GameObject gameObject = o as GameObject;
                    if (gameObject.IsDestroying())
                    {
                        Debug.LogError("Destroying object multiple times. Don't use DestroyImmediate on the same object in OnDisable or OnDestroy.");
                        return;
                    }

                    if (gameObject.IsActivating())
                    {
                        Debug.LogError("Cannot destroy GameObject while it is being activated or deactivated.");
                        return;
                    }

                    Transform parent = gameObject.transform;
                    if (parent)
                    {
                        parent = parent.parent;
                        if (parent && parent.gameObject.IsActivating())
                        {
                            Debug.LogError("Cannot destroy GameObject while it is being activated or deactivated.");
                            return;
                        }
                    }
                    DestroyGameObjectHierarchy(gameObject);
                }
            }
        }
Example #2
0
 /// <summary>
 /// ARCore looks for certain things as soon as the component is enabled, so we have to delay
 /// it. First, we disable the object hierarchy in question. Then, we execute the code to
 /// create the ARCore objects. Finally, we re-enable the hierarchy.
 /// </summary>
 /// <param name="src"></param>
 /// <param name="act"></param>
 public static void WithLock(this Component src, Action act)
 {
     src.Deactivate();
     act();
     src.Activate();
 }