コード例 #1
0
    void CheckDrawLine()
    {
        bool wipe = false;

        if (Input.GetMouseButtonDown(0))
        {
            //Started drawing!
            drawing = true;

            //start drawing lines
            Ray        ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000, mask))
            {
                Nodes.Node n = Nodes.FindNearestNode(hit.point);

                //check this is a valid pos
                if (Nodes.CheckValidNode(n))
                {
                    lineId      = Lines.RegisterNewLine();
                    startPos    = n.pos;
                    startNodeId = n.uId;
                    Lines.AddPoint(n, lineId);
                }
                else
                {
                    //else, fail out
                    drawing = false;
                }
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            if (drawing)
            {
                //Add last point
                Ray        ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 1000, mask))
                {
                    Nodes.Node n = Nodes.FindNearestNode(hit.point);
                    if (n.uId == startNodeId)
                    {
                        wipe = true;
                    }
                    else
                    {
                        //check this is a valid pos
                        if (Nodes.CheckValidNode(n))
                        {
                            Lines.AddPoint(n, lineId);
                        }
                        else
                        {
                            wipe = true;
                        }
                    }
                }

                if (wipe)
                {
                    //kill the line
                    Lines.DeleteLine(lineId);
                }
                else
                {
                    lastLineId = lineId;
                }
                //Released!
                drawing = false;
                lineId  = -1;
            }
        }
        if (Input.GetMouseButton(0))
        {
            if (drawing)
            {
                Ray        ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 1000, mask))
                {
                    Vector3 point = hit.point;
                    Debug.DrawLine(startPos, point, Color.red);
                }
            }
        }
    }