示例#1
0
    void Update()
    {
        if (answered)
        {
            return;
        }

        answerButton.interactable = answerArea.vertices.Count >= 3;

        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        ray           = ray.ReverseRay();
        ray.direction = -ray.origin;
        RaycastHit hit;

        if (isDragging)
        {
            if (Input.GetMouseButtonUp(0))
            {
                answerArea.vertices[dragIndex] = dragObject.transform.position;
                isDragging = false;
                dragObject = null;
                indicator.SetActive(true);
            }
            else if (Input.GetKeyDown(KeyCode.Escape))
            {
                dragObject.transform.position = dragStartPost;
                isDragging = false;
                dragObject = null;
                dirty      = true;
                indicator.SetActive(true);
            }
            else if (Physics.Raycast(ray, out hit, 100f))
            {
                //NOTE(Simon): "ray.direction * .5f" is a patch for an annoying bug I couldn't figure out.
                dragObject.transform.position = hit.point + ray.direction * .5f;
                dirty = true;
                //NOTE(Simon): indicator is not visible at this point, but still update its position to prevent visual glitch on reappearance
                indicator.transform.position = hit.point;
            }
        }
        else if (Physics.Raycast(ray, out hit, 100f, LayerMask.GetMask("Area")))
        {
            eligibleForPlacement       = false;
            Cursors.isOverridingCursor = true;
            Cursor.SetCursor(Cursors.Instance.CursorDrag, new Vector2(15, 15), CursorMode.Auto);
            indicator.SetActive(false);

            if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                isDragging    = true;
                dragStartPost = hit.transform.position;
                dragObject    = hit.transform.gameObject;
                dragIndex     = answerArea.vertices.IndexOf(dragStartPost);
            }
        }
        else if (Physics.Raycast(ray, out hit, 100f))
        {
            Cursors.isOverridingCursor = false;
            indicator.SetActive(true);
            indicator.transform.position = hit.point;

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                answered   = true;
                answerArea = null;
            }

            if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                //NOTE(Simon): Store if mouse button went down at a valid time (i.e. not while dragging)
                eligibleForPlacement = true;
            }
            if (eligibleForPlacement && Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                answerArea.vertices.Add(hit.point);

                areaPoints.Add(NewPoint(hit.point));

                dirty = true;
            }
        }

        if (dirty)
        {
            var vertices = new Vector3[areaPoints.Count];
            for (int i = 0; i < areaPoints.Count; i++)
            {
                vertices[i] = areaPoints[i].transform.position;
            }

            areaRenderer.SetVertices(vertices);

            dirty = false;
        }
    }