コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        float   halfSize      = size * 0.5f;

        if (Input.GetMouseButtonDown(0))
        {
            if (mousePosition.x >= origin.x - halfSize && mousePosition.x <= origin.x + halfSize &&
                mousePosition.y >= origin.y - halfSize && mousePosition.y <= origin.y + halfSize)
            {
                float qsize = halfSize / 2;
                if (mousePosition.x > origin.x - qsize && mousePosition.x < origin.x + qsize)
                {
                    if (mousePosition.y > origin.y + qsize)
                    {
                        drag = TypeDrag.TOP;
                    }
                    else if (mousePosition.y < origin.y - qsize)
                    {
                        drag = TypeDrag.BOT;
                    }
                }
                else if (mousePosition.y > origin.y - qsize && mousePosition.y < origin.y + qsize)
                {
                    if (mousePosition.x > origin.x + qsize)
                    {
                        drag = TypeDrag.RIGHT;
                    }
                    else if (mousePosition.x < origin.x - qsize)
                    {
                        drag = TypeDrag.LEFT;
                    }
                }
                else if (mousePosition.x < origin.x - qsize)
                {
                    if (mousePosition.y > origin.y + qsize)
                    {
                        drag = TypeDrag.TOP_LEFT;
                    }
                    else if (mousePosition.y < origin.y - qsize)
                    {
                        drag = TypeDrag.BOT_LEFT;
                    }
                }
                else if (mousePosition.x > origin.x + qsize)
                {
                    if (mousePosition.y > origin.y + qsize)
                    {
                        drag = TypeDrag.TOP_RIGHT;
                    }
                    else if (mousePosition.y < origin.y - qsize)
                    {
                        drag = TypeDrag.BOT_RIGHT;
                    }
                }
                start = mousePosition;
                UpdateType();
            }
        }

        if (Input.GetMouseButton(0))
        {
            if (drag != TypeDrag.NONE && mousePosition != start)
            {
                back.SetActive(true);
                Vector2 offset = mousePosition - start;
                if (drag == TypeDrag.TOP)
                {
                    float offsetY = offset.y;
                    if (isLimitDrag)
                    {
                        if (offsetY > 0)
                        {
                            offsetY = 0;
                        }
                        else if (offsetY < -size)
                        {
                            offsetY = -size;
                        }
                    }

                    Vector2 anchor = new Vector2(origin.x, origin.y + halfSize);
                    mask.transform.position    = anchor + new Vector2(0, halfSize + offsetY / 2);
                    back.transform.position    = new Vector2(origin.x, origin.y + size + offsetY);
                    back.transform.eulerAngles = Vector3.zero;
                }
                else if (drag == TypeDrag.RIGHT)
                {
                    float offsetX = offset.x;
                    if (isLimitDrag)
                    {
                        if (offsetX > 0)
                        {
                            offsetX = 0;
                        }
                        else if (offsetX < -size)
                        {
                            offsetX = -size;
                        }
                    }

                    Vector2 anchor = new Vector2(origin.x + halfSize, origin.y);
                    mask.transform.position    = anchor + new Vector2(halfSize + offsetX / 2, 0);
                    back.transform.position    = new Vector2(origin.x + size + offsetX, origin.y);
                    back.transform.eulerAngles = Vector3.zero;
                }
                else if (drag == TypeDrag.BOT)
                {
                    float offsetY = offset.y;
                    if (isLimitDrag)
                    {
                        if (offsetY < 0)
                        {
                            offsetY = 0;
                        }
                        else if (offsetY > size)
                        {
                            offsetY = size;
                        }
                    }

                    Vector2 anchor = new Vector2(origin.x, origin.y - halfSize);
                    mask.transform.position    = anchor - new Vector2(0, halfSize - offsetY / 2);
                    back.transform.position    = new Vector2(origin.x, origin.y - size + Mathf.Abs(offsetY));
                    back.transform.eulerAngles = Vector3.zero;
                }
                else if (drag == TypeDrag.LEFT)
                {
                    float offsetX = offset.x;
                    if (isLimitDrag)
                    {
                        if (offsetX < 0)
                        {
                            offsetX = 0;
                        }
                        else if (offsetX > size)
                        {
                            offsetX = size;
                        }
                    }

                    Vector2 anchor = new Vector2(origin.x - halfSize, origin.y);
                    mask.transform.position    = anchor - new Vector2(halfSize - offsetX / 2, 0);
                    back.transform.position    = new Vector2(origin.x - size + offsetX, origin.y);
                    back.transform.eulerAngles = Vector3.zero;
                }
                else if (drag == TypeDrag.TOP_LEFT)
                {
                    float angle = -45;
                    float dy    = Mathf.Abs(offset.magnitude);
                    if (isLimitDrag && dy > size)
                    {
                        dy = size;
                    }
                    float   curSize = size * 2;
                    Vector2 anchor  = new Vector3(origin.x - halfSize, origin.y + halfSize);
                    mask.transform.position         = anchor - new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad) * size, Mathf.Sin(angle * Mathf.Deg2Rad) * size) + new Vector2(dy, -dy) / 2;
                    mask.transform.localEulerAngles = Vector3.forward * angle;

                    back.transform.position    = new Vector2(origin.x - size + dy, origin.y + size - dy);
                    back.transform.eulerAngles = Vector3.forward * -90;
                }
                else if (drag == TypeDrag.TOP_RIGHT)
                {
                    float angle = 45;
                    float dy    = Mathf.Abs(offset.magnitude);
                    if (isLimitDrag && dy > size)
                    {
                        dy = size;
                    }
                    float   curSize = size * 2;
                    Vector2 anchor  = new Vector3(origin.x + halfSize, origin.y + halfSize);
                    mask.transform.position         = anchor + new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad) * size, Mathf.Sin(angle * Mathf.Deg2Rad) * size) + new Vector2(-dy, -dy) / 2;
                    mask.transform.localEulerAngles = Vector3.forward * angle;

                    back.transform.position    = new Vector2(origin.x + size - dy, origin.y + size - dy);
                    back.transform.eulerAngles = Vector3.forward * 90;
                }
                else if (drag == TypeDrag.BOT_LEFT)
                {
                    float angle = 45;
                    float dy    = Mathf.Abs(offset.magnitude);
                    if (isLimitDrag && dy > size)
                    {
                        dy = size;
                    }
                    float   curSize = size * 2;
                    Vector2 anchor  = new Vector3(origin.x - halfSize, origin.y - halfSize);
                    mask.transform.position         = anchor - new Vector2(Mathf.Sin(angle * Mathf.Deg2Rad) * size, Mathf.Cos(angle * Mathf.Deg2Rad) * size) + new Vector2(dy, dy) / 2;
                    mask.transform.localEulerAngles = Vector3.forward * angle;

                    back.transform.position    = new Vector2(origin.x - size + dy, origin.y - size + dy);
                    back.transform.eulerAngles = Vector3.forward * -90;
                }
                else if (drag == TypeDrag.BOT_RIGHT)
                {
                    float angle = -45;
                    float dy    = Mathf.Abs(offset.magnitude);
                    if (isLimitDrag && dy > size)
                    {
                        dy = size;
                    }
                    float   curSize = size * 2;
                    Vector2 anchor  = new Vector3(origin.x + halfSize, origin.y - halfSize);
                    mask.transform.position         = anchor - new Vector2(Mathf.Sin(angle * Mathf.Deg2Rad) * size, Mathf.Cos(angle * Mathf.Deg2Rad) * size) + new Vector2(-dy, dy) / 2;
                    mask.transform.localEulerAngles = Vector3.forward * angle;

                    back.transform.position    = new Vector2(origin.x + size - dy, origin.y - size + dy);
                    back.transform.eulerAngles = Vector3.forward * 90;
                }
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            drag = TypeDrag.NONE;
            NewTurn();
        }
    }
コード例 #2
0
    public void Update()
    {
        Vector2 mousePosition = Input.mousePosition - new Vector3(canvas.pixelRect.width / 2, canvas.pixelRect.height / 2, 0);

        Debug.Log("Goc : " + new Vector2(frontX - frontW / 2, frontY - frontH / 2));
        if (Input.GetMouseButtonDown(0))
        {
            if (mousePosition.x >= frontX - frontW / 2 && mousePosition.x <= frontX + frontW / 2 &&
                mousePosition.y >= frontY - frontH / 2 && mousePosition.y <= frontY + frontH / 2)
            {
                if (mousePosition.x < frontX && mousePosition.y > frontY)
                {
                    drag = TypeDrag.TOP_LEFT;
                }
                else if (mousePosition.x > frontX && mousePosition.y > frontY)
                {
                    drag = TypeDrag.TOP_RIGHT;
                }
                else if (mousePosition.x < frontX && mousePosition.y < frontY)
                {
                    drag = TypeDrag.BOT_LEFT;
                }
                else if (mousePosition.x > frontX && mousePosition.y < frontY)
                {
                    drag = TypeDrag.BOT_RIGHT;
                }
                start = mousePosition;
            }
        }

        if (Input.GetMouseButton(0))
        {
            if (drag != TypeDrag.NONE && mousePosition != start)
            {
                // Vector2 offset = mousePosition - start;
                Vector2 offset = mousePosition - new Vector2(frontX - frontW / 2, frontY - frontH / 2);
                float   half   = offset.magnitude * 0.5f;
                Debug.Log(offset);

                if (drag == TypeDrag.TOP_LEFT)
                {
                }
                else if (drag == TypeDrag.TOP_RIGHT)
                {
                }
                else if (drag == TypeDrag.BOT_LEFT)
                {
                    float angleForX = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
                    float angleForY = 90 - angleForX;
                    float x         = half / Mathf.Cos(angleForX * Mathf.Deg2Rad);
                    float y         = half / Mathf.Cos(angleForY * Mathf.Deg2Rad);
                    // Debug.Log(x + "/" + y);
                    x = Mathf.Clamp(x, frontX - frontW * 0.5f, frontX + frontW * 0.5f);
                    y = Mathf.Clamp(y, frontY - frontH * 0.5f, frontY + frontH * 0.5f);
                    // float angle = Mathf.Atan2(y, x);
                    // float angle = Mathf.Atan2(mousePosition.y, mousePosition.x);
                    // float angle = 90 - Mathf.Atan2(y, x) * Mathf.Rad2Deg;
                    // mask.transform.eulerAngles = Vector3.forward * (angle);

                    // mask.pivot = new Vector2(0f, 0.5f);
                    Vector2 maskPos = Vector2.zero - new Vector2(offset.x, offset.y) * 0.5f;
                    // Debug.Log("Mask pos : " + maskPos);
                    // maskPos = mousePosition;
                    // mask.transform.position = maskPos;

                    // back.pivot = new Vector2(0.5f, 0.5f);
                    // back.pivot = new Vector2(1f, 0f);
                    // back.transform.position = mousePosition;
                    // float angleBack = Mathf.Atan2(mousePosition.x - x, mousePosition.y);
                    // float angleBack = Mathf.Atan2(mousePosition.y, mousePosition.x - x);
                    // float angleBack = Mathf.Atan2(-mousePosition.y, mousePosition.x);
                    // back.transform.localPosition = new Vector2(-300 + half, back.transform.localPosition.y);
                    // back.transform.localEulerAngles = -(Vector3.forward * (angleBack * Mathf.Rad2Deg));
                }
                else if (drag == TypeDrag.BOT_RIGHT)
                {
                }
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            drag = TypeDrag.NONE;
        }
    }