예제 #1
0
            public virtual void OnValidate()
            {
                _graphic.isRepresentationDirty = true;

                foreach (var data in _graphic._featureData)
                {
                    data.MarkFeatureDirty();
                }

                if (!Application.isPlaying)
                {
                    if (_graphic.isAttachedToGroup && !_graphic.transform.IsChildOf(_graphic._attachedRenderer.transform))
                    {
                        _graphic.OnDetachedFromGroup();
                    }

                    if (_graphic.isAttachedToGroup)
                    {
                        _graphic._attachedRenderer.editor.ScheduleRebuild();
                        _graphic._preferredRendererType = _graphic.attachedGroup.renderingMethod.GetType();
                    }
                }
                else
                {
                    var group = _graphic.attachedGroup;
                    if (group != null)
                    {
                        if (!group.graphics.Contains(_graphic))
                        {
                            _graphic.OnDetachedFromGroup();
                            group.TryAddGraphic(_graphic);
                        }
                    }
                }
            }
        /// <summary>
        /// Tries to remove the given graphic from this group.  This can safely be called
        /// during runtime or edit time.  This method can fail under the following
        /// conditions:
        ///    - The graphic is not attached to this group.
        ///    - It is runtime and add/remove is not supported by this group.
        ///
        /// At runtime the actual detachment is delayed until LateUpdate for efficiency
        /// reasons.  Expect that even if this method returns true that the graphic will
        /// not actually be detached until the end of LateUpdate.
        /// </summary>
        public bool TryRemoveGraphic(LeapGraphic graphic)
        {
            Assert.IsNotNull(graphic);

            if (!addRemoveSupportedOrEditTime())
            {
                return(false);
            }

#if UNITY_EDITOR
            if (Application.isPlaying)
#endif
            {
                if (_toDetach.Contains(graphic))
                {
                    return(false);
                }
                if (_toAttach.Contains(graphic))
                {
                    graphic.CancelWillBeAttached();
                    graphic.isRepresentationDirty = true;
                    _toAttach.Remove(graphic);
                    return(true);
                }
            }

            int graphicIndex = _graphics.IndexOf(graphic);
            if (graphicIndex < 0)
            {
                return(false);
            }

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Undo.RecordObject(graphic, "Removed graphic from group");
                Undo.RecordObject(_renderer, "Removed graphic from group");

                graphic.OnDetachedFromGroup();
                _graphics.RemoveAt(graphicIndex);

                RebuildFeatureData();
                RebuildFeatureSupportInfo();

                if (_renderer.space != null)
                {
                    _renderer.space.RebuildHierarchy();
                    _renderer.space.RecalculateTransformers();
                }

                _renderer.editor.ScheduleRebuild();
            }
            else
#endif
            {
                if (_toDetach.Contains(graphic))
                {
                    return(false);
                }

                graphic.NotifyWillBeDetached(this);
                _toDetach.Add(graphic);
            }

            return(true);
        }
        /// <summary>
        /// Tries to add the given graphic to this group.  This can safely be called
        /// during runtime or edit time.  This method can fail under the following
        /// conditions:
        ///    - The graphic is already attached to this group.
        ///    - The graphic is already attached to a different group.
        ///    - It is runtime and add/remove is not supported by this group.
        ///
        /// At runtime the actual attachment is delayed until LateUpdate for efficiency
        /// reasons.  Expect that even if this method returns true that the graphic will
        /// not actually be attached until the end of LateUpdate.
        /// </summary>
        public bool TryAddGraphic(LeapGraphic graphic)
        {
            Assert.IsNotNull(graphic);

            if (graphic.willbeAttached || (graphic.isAttachedToGroup && !graphic.willbeDetached))
            {
                return(false);
            }

            if (!addRemoveSupportedOrEditTime())
            {
                return(false);
            }

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Undo.RecordObject(graphic, "Added graphic to group");
            }
            else
#endif
            {
                if (_toAttach.Contains(graphic))
                {
                    return(false);
                }
                if (_toDetach.Contains(graphic))
                {
                    graphic.CancelWillBeDetached();
                    graphic.isRepresentationDirty = true;
                    _toDetach.Remove(graphic);
                    return(true);
                }
            }

            if (_graphics.Contains(graphic))
            {
                if (graphic.attachedGroup == null)
                {
                    //detatch and re-add, it forgot it was attached!
                    //This can easily happen at edit time due to prefab shenanigans
                    graphic.OnDetachedFromGroup();
                    _graphics.Remove(graphic);
                }
                else
                {
                    Debug.LogWarning("Could not add graphic because it was already a part of this group.");
                    return(false);
                }
            }

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                int newIndex = _graphics.Count;
                _graphics.Add(graphic);

                LeapSpaceAnchor anchor = _renderer.space == null ? null : LeapSpaceAnchor.GetAnchor(graphic.transform);

                RebuildFeatureData();
                RebuildFeatureSupportInfo();

                graphic.OnAttachedToGroup(this, anchor);

                if (_renderer.space != null)
                {
                    _renderer.space.RebuildHierarchy();
                    _renderer.space.RecalculateTransformers();
                }

                _renderer.editor.ScheduleRebuild();
            }
            else
#endif
            {
                if (_toAttach.Contains(graphic))
                {
                    return(false);
                }

                graphic.NotifyWillBeAttached(this);
                _toAttach.Add(graphic);
            }

            return(true);
        }