コード例 #1
0
    /// <summary>
    /// Code to run in the legacy (Immediate Mode) GUI layer (Called by
    /// MonoBehaviour every UI refresh). This code handles displaying vertices,
    /// and checking when they are clicked.
    /// </summary>
    private void OnGUI()
    {
        foreach (ISelectable point in selMesh.GetAllPoints())
        {
            float widgetSize = 10;

            Vector3 vertScreenPos = Camera.main.WorldToScreenPoint(point.Pos);
            float   distToMouse   = Vector2.SqrMagnitude(vertScreenPos - Input.mousePosition);
            vertScreenPos.y = Screen.height - vertScreenPos.y;
            vertScreenPos  -= new Vector3(1, 1, 0) * widgetSize / 2;

            float mouseDistThreshold = selectionMode == 0 ? 1000 : 3000;
            if (vertScreenPos.z > 0)
            {
                if (renderAllPoints ||
                    point.Selected ||
                    (distToMouse < mouseDistThreshold && PointIsVisible(point)))
                {
                    GUIStyle guiVertStyle = point.Selected ? guiVertSel : guiVertUnsel;
                    GUI.Label(new Rect(vertScreenPos.x, vertScreenPos.y, widgetSize, widgetSize), "", guiVertStyle);
                    if (distToMouse < 64 && Input.GetMouseButtonDown(0) && GUIreadyForClick)
                    {
                        GUIreadyForClick = false;

                        OnPointClicked(point, Input.GetKey(KeyCode.LeftShift));
                    }
                }
            }
        }

        if (!Input.GetMouseButtonDown(0))
        {
            GUIreadyForClick = true;
        }

        bool PointIsVisible(ISelectable point)
        {
            Vector3 camPos            = Camera.main.transform.position;
            float   distanceThreshold = 0.95f;
            //RaycastHit hit;
            bool didHit = Physics.Raycast(camPos, point.Pos - camPos, out RaycastHit hit);

            if (hit.distance / Vector3.Distance(camPos, point.Pos) > distanceThreshold || !didHit)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
    }