Beispiel #1
0
    void RemovePiece()
    {
        int controlID = GUIUtility.GetControlID(FocusType.Keyboard);

        if (GUIUtility.hotControl == 0)
        {
            HandleUtility.AddDefaultControl(controlID);
        }

        if (m_CurrentInstance != null)
        {
            m_CurrentInstance.gameObject.SetActive(false);
        }

        var         mousePos     = Event.current.mousePosition;
        PuzzlePiece closestPiece = null;
        Bounds      closestBound = new Bounds();

        float closestSqrDist = float.MaxValue;

        for (int i = 0; i < m_PuzzleLayout.pieces.Length; ++i)
        {
            PuzzlePiece r = m_PuzzleLayout.pieces[i];

            if (r == null)
            {
                continue;
            }

            //This bit is inneficient, but should be enough for our purpose here in the kit. In very big scene
            //it could slow down the editing process. Bound should probably be stored in local space or better should
            //find a way to use the built-in picking but that require more complexity than necessary for those small kit
            Bounds b = new Bounds();

            Renderer[] renderers = r.GetComponentsInChildren <Renderer>();

            if (renderers.Length > 0)
            {
                for (int k = 0; k < renderers.Length; ++k)
                {
                    if (k == 0)
                    {
                        b = renderers[k].bounds;
                    }
                    else
                    {
                        b.Encapsulate(renderers[k].bounds);
                    }
                }
            }
            else
            {
                //if the piece got no collider, it may be an "empty" piece used to introduce gap, so instead look for
                //a collider to find it's size.
                Collider[] colliders = r.GetComponentsInChildren <Collider>();
                for (int k = 0; k < colliders.Length; ++k)
                {
                    if (k == 0)
                    {
                        b = colliders[k].bounds;
                    }
                    else
                    {
                        b.Encapsulate(colliders[k].bounds);
                    }
                }
            }

            var guiPts = HandleUtility.WorldToGUIPoint(b.center);

            float dist = (guiPts - mousePos).sqrMagnitude;

            if (dist < closestSqrDist)
            {
                closestSqrDist = dist;
                closestPiece   = r;
                closestBound   = b;
            }
        }

        if (closestPiece != null)
        {
            if (Event.current.type == EventType.Repaint)
            {
                MeshFilter filter = closestPiece.GetComponentInChildren <MeshFilter>();

                if (filter != null)
                {
                    m_HighlightMaterial.SetPass(0);
                    Graphics.DrawMeshNow(filter.sharedMesh, closestPiece.transform.localToWorldMatrix);
                }

                Handles.DrawWireCube(closestBound.center, closestBound.size);
            }
            else if (Event.current.type == EventType.MouseUp && Event.current.button == 0 && GUIUtility.hotControl == 0)
            {
                closestPiece.Removed();
                Undo.DestroyObjectImmediate(closestPiece.gameObject);
            }
            else
            {
                SceneView.currentDrawingSceneView.Repaint();
            }
        }
    }