鼠标拖动时绘制UNITY矩形

时间:2018-12-15 19:58:57

标签: c# unity3d

基于鼠标悬停绘制矩形轮廓的正确方法是什么? 选择单位时会影响您进入策略游戏。 运行Unity 2018.3.0f。也许我的代码可以稍作更改。我找不到完成的事情。作为第二个选项,我可以使用LineRenderer并基于该行创建一个rect。添加将构成矩形的行。

这是一个示例: enter image description here

这是我想出的,但是由于某种原因,它的Y坐标错误。

using UnityEngine;
using System.Collections;
using System;

public class MouseDragSelect : MonoBehaviour
{

    private Rect position = new Rect(193, 148, 249-193, 148-104);
    public Color color = Color.green;
    private Vector3[] mousePositions = new Vector3[2];
    private bool draggingMouse = false;
    private bool drawRect = false;
    public float timer = 1.2f;

    void OnGUI()
    {
        if (drawRect)
        {
            DrawRectangle(position, 1, color);
        }
    }

    void DrawRectangle(Rect area, int frameWidth, Color color)
    {
        //Create a one pixel texture with the right color
        var texture = new Texture2D(1, 1);
        texture.SetPixel(0, 0, color);
        texture.Apply();

        Rect lineArea = area;
        lineArea.height = frameWidth; //Top line
        GUI.DrawTexture(lineArea, texture);
        lineArea.y = area.yMax - frameWidth; //Bottom
        GUI.DrawTexture(lineArea, texture);
        lineArea = area;
        lineArea.width = frameWidth; //Left
        GUI.DrawTexture(lineArea, texture);
        lineArea.x = area.xMax - frameWidth;//Right
        GUI.DrawTexture(lineArea, texture);
    }

    void reset() {
        drawRect = false;
        mousePositions[0] = new Vector3();
        mousePositions[1] = new Vector3();
        timer = 1.2f;
        draggingMouse = false;
    }
    private void Update()
    {
        if (drawRect)
        {
            if (timer > 0.1)
            {
                timer -= 1 * Time.deltaTime;
            } else {
               reset();
            }
        }
        if(Input.GetMouseButtonDown(0)) {
            if(!draggingMouse){
                mousePositions[0] = Input.mousePosition;
                print("x start:" + mousePositions[0].x);
                print("y start:" + mousePositions[0].y);
            }
            draggingMouse = true;
        }
        if(Input.GetMouseButtonUp(0)) {
            if(draggingMouse) {
                mousePositions[1] = Input.mousePosition;
                float width = Math.Abs(mousePositions[1].x - mousePositions[0].x);
                float height = Math.Abs(mousePositions[1].y - mousePositions[0].y);
                float x = mousePositions[0].x;
                float y = mousePositions[0].y;
               // print("width:" + width);
               // print("height:" + height);
                print("x end:" + mousePositions[1].x);
                print("y end:" + mousePositions[1].y);
                position = new Rect(x, y, width, height);
               // print("Got last mouse position!");
                drawRect = true;
            }
        }
    }
}

尝试选择角色周围的区域。获得奇怪的Y坐标。 enter image description here

以下解决方案也不起作用:(因为我收到奇怪的Y坐标

            float x = Math.Min(mousePositions[0].x, mousePositions[1].x);
            float y = Math.Min(mousePositions[0].y, mousePositions[1].y);
            float width = Math.Max(mousePositions[0].x, mousePositions[1].x) - x;
            float height = Math.Max(mousePositions[0].y, mousePositions[1].y) - y;

2 个答案:

答案 0 :(得分:1)

因此,对于您来说,鼠标和屏幕的屏幕空间和世界空间是不同的。将y更改为Screen.height-mousePosition.y。这将为您解决

答案 1 :(得分:0)

GL.LoadPixelMatrix提供了简单而适当的工作流程来做到这一点。