예제 #1
0
        private void HitTest()
        {
            if (m_rectTransform.sizeDelta.magnitude < 25f)
            {
                return;
            }

            Vector3 center = (m_startMousePosition + (Vector3)Window.Pointer.ScreenPoint) / 2;

            center.z = 0.0f;
            Bounds selectionBounds = new Bounds(center, m_rectTransform.sizeDelta);

            SelectionBounds = selectionBounds;

            FilteringArgs        filteringArgs = new FilteringArgs();
            HashSet <GameObject> selection;

            Renderer[] renderers = FindObjectsOfType <Renderer>();

            if (MethodOverride == BoxSelectionMethod.PixelPerfectDepthTest)
            {
                selection = new HashSet <GameObject>(FilterObjects(filteringArgs, PixelPerfectDepthTest(renderers, SelectionBounds)).Select(rend => rend.gameObject));
            }
            else
            {
                selection = new HashSet <GameObject>();

                Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(Window.Camera);
                for (int i = 0; i < renderers.Length; ++i)
                {
                    Renderer   r      = renderers[i];
                    Bounds     bounds = r.bounds;
                    GameObject go     = r.gameObject;
                    TrySelect(ref selectionBounds, selection, filteringArgs, ref bounds, go, frustumPlanes);
                }
            }

            SpriteGizmo[] spriteGizmos = FindObjectsOfType <SpriteGizmo>();
            for (int i = 0; i < spriteGizmos.Length; ++i)
            {
                SpriteGizmo spriteGizmo = spriteGizmos[i];
                bool        select      = TransformCenter(ref selectionBounds, spriteGizmo.transform);
                if (select)
                {
                    Filter(selection, filteringArgs, spriteGizmo.gameObject);
                }
            }

            if (Selection != null)
            {
                Selection(this, new BoxSelectionArgs {
                    GameObjects = selection.ToArray()
                });
            }
            else
            {
                Editor.Selection.objects = selection.ToArray();
            }
        }
        private void HitTest()
        {
            if (m_rectTransform.sizeDelta.magnitude < 25f)
            {
                return;
            }

            Vector3 center = (m_startMousePosition + (Vector3)Window.Pointer.ScreenPoint) / 2;

            center.z = 0.0f;
            Bounds selectionBounds = new Bounds(center, m_rectTransform.sizeDelta);

            SelectionBounds = selectionBounds;

            FilteringArgs        filteringArgs = new FilteringArgs();
            HashSet <GameObject> selection;

            Renderer[] renderers = FindObjectsOfType <Renderer>();

            if (MethodOverride == BoxSelectionMethod.PixelPerfectDepthTest)
            {
                Vector2 min    = SelectionBounds.min;
                Vector2 max    = SelectionBounds.max;
                Canvas  canvas = Window.GetComponentInParent <Canvas>();

                RectTransform sceneOutput = (RectTransform)Window.GetComponent <RectTransform>().GetChild(0);

                RectTransformUtility.ScreenPointToLocalPointInRectangle(sceneOutput, min, canvas.worldCamera, out min);
                min.y = sceneOutput.rect.height - min.y;
                RectTransformUtility.ScreenPointToLocalPointInRectangle(sceneOutput, max, canvas.worldCamera, out max);
                max.y = sceneOutput.rect.height - max.y;

                Rect rect = new Rect(new Vector2(Mathf.Min(min.x, max.x), Mathf.Min(min.y, max.y)), new Vector2(Mathf.Abs(max.x - min.x), Mathf.Abs(max.y - min.y)));
                rect.x += Window.Camera.pixelRect.x;
                rect.y += canvas.pixelRect.height - (Window.Camera.pixelRect.y + Window.Camera.pixelRect.height);

                IEnumerable <GameObject> gameObjects = BoxSelectionRenderer.PickObjectsInRect(Window.Camera, rect, renderers, Mathf.RoundToInt(canvas.pixelRect.width), Mathf.RoundToInt(canvas.pixelRect.height)).Select(r => r.gameObject);
                selection = new HashSet <GameObject>();
                foreach (GameObject go in gameObjects)
                {
                    if (!selection.Contains(go))
                    {
                        if (Filtering != null)
                        {
                            filteringArgs.Object = go;
                            Filtering(this, filteringArgs);
                            if (!filteringArgs.Cancel)
                            {
                                selection.Add(go);
                            }
                            filteringArgs.Reset();
                        }
                        else
                        {
                            selection.Add(go);
                        }
                    }
                }
            }
            else
            {
                selection = new HashSet <GameObject>();

                Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(Window.Camera);
                for (int i = 0; i < renderers.Length; ++i)
                {
                    Renderer   r      = renderers[i];
                    Bounds     bounds = r.bounds;
                    GameObject go     = r.gameObject;
                    TrySelect(ref selectionBounds, selection, filteringArgs, ref bounds, go, frustumPlanes);
                }
            }

            SpriteGizmo[] spriteGizmos = FindObjectsOfType <SpriteGizmo>();
            for (int i = 0; i < spriteGizmos.Length; ++i)
            {
                SpriteGizmo spriteGizmo = spriteGizmos[i];
                bool        select      = TransformCenter(ref selectionBounds, spriteGizmo.transform);
                if (select)
                {
                    Filter(selection, filteringArgs, spriteGizmo.gameObject);
                }
            }

            if (Selection != null)
            {
                Selection(this, new BoxSelectionArgs {
                    GameObjects = selection.ToArray()
                });
            }
            else
            {
                Editor.Selection.objects = selection.ToArray();
            }
        }