【问题标题】:Change LineRenderer draw order更改 LineRenderer 绘制顺序
【发布时间】:2018-06-29 09:53:03
【问题描述】:

我编写了以下脚本,它创建线 LineRenderer 对象并模拟 Unity3d 中的基本“绘画”能力。这工作得很好,但是我似乎无法让它正确地渲染线条。哪个出现在前面,哪个出现在后面似乎是随机的。

例如:

在此图像中,线条按黑色、橙色、蓝色、黄色、红色、绿色的顺序绘制。但是,正如您所看到的,这不是它们呈现的顺序。

void Update ()
    {
        //Create new Line on left mouse click(down)
        if(Input.GetButtonDown("Fire1"))
        {
            prevPos     = Input.mousePosition;
            points = 2;
            //check if there is no line renderer created
            if(line == null){
                //create the line
                createLine();
            }
            //get the mouse position
            mousePos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,
                Input.mousePosition.y, Camera.main.nearClipPlane));

            //set the start point and end point of the line renderer
            line.SetPosition(0,mousePos);
            line.SetPosition(1,mousePos);
        }
        //if line renderer exists and left mouse button is click exited (up)
        else if(Input.GetButtonUp("Fire1") && line)
        {
            mousePos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,
                Input.mousePosition.y, Camera.main.nearClipPlane));
            //set the end point of the line renderer to current mouse position
            line.SetPosition(points-1,mousePos);
            //set line as null once the line is created
            line = null;
            currLines++;
        }
        //if mouse button is held clicked and line exists
        else if(Input.GetButton("Fire1")  && line)
        {
            mousePos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,
                Input.mousePosition.y, Camera.main.nearClipPlane));
            //set the end position as current position but dont set line as null as the mouse click is not exited
            line.SetPosition(points-1, mousePos);

            currPos = Input.mousePosition;
            if (currPos != prevPos) {
                prevPos = currPos;
                points = points + 1;
                line.SetVertexCount (points);
                line.SetPosition(points-1, mousePos);
            }

        }

        if (clearing) {
            GameObject[] gameObjects = GameObject.FindGameObjectsWithTag ("lines");

            for(var i = 0 ; i < gameObjects.Length ; i ++)
            {
                Destroy(gameObjects[i]);
            }
        }
    }

    //method to create line
    private void createLine()
    {
        Material newm = new Material(Shader.Find("Unlit/Color")); 
        newm.SetColor("_Color", lineColor);
        //create a new empty gameobject and line renderer component
        line = new GameObject("Line"+currLines).AddComponent<LineRenderer>();
        line.tag = "lines";
        //assign the material to the line
        line.material =  newm;
        //set the number of points to the line
        line.SetVertexCount(2);

        //set the width
        line.SetWidth(((float)lineSize)/1000, ((float)lineSize)/1000);
        //render line to the world origin and not to the object's position
        line.useWorldSpace = true;  

    }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您可以使用 “排序层” 和 “层内排序” 属性来执行此操作。 "Order In Layer" 推荐用于此,因为您只需要更改它们的 int 值。仅在必要时使用“排序层”。具有最高“Order In Layer” 值的LineRenderer 将被绘制在其他LineRenderers 之上。

    通过脚本更改“Order In Layer”:

    你的四行:

    public LineRenderer line1;
    public LineRenderer line2;
    public LineRenderer line3;
    public LineRenderer line4;
    

    在屏幕上排列它们:

    line1.sortingOrder = 3;
    line2.sortingOrder = 2;
    line3.sortingOrder = 1;
    line4.sortingOrder = 0;
    

    这意味着 line 1 将被放置在所有其他行的顶部,然后是 line 2,然后是 line 3,最后是第 4 行。因此,第 4 行 将被放置在所有行的下方。 这是执行此操作的正确方法,它适用于 2D 和 3D 矢量位置。

    【讨论】:

      【解决方案2】:

      我猜你是在 XY 平面上绘制线条渲染器。那么Z将表示线渲染器的depth。在 int 类型的脚本中取一个深度变量。每当绘制一条线时,将其增加 1。每当您绘制线时,根据深度设置它的 points Z。您可以在 SetPosition 函数调用期间执行此操作。希望这是有道理的。

      【讨论】:

        猜你喜欢
        • 2019-07-02
        • 1970-01-01
        • 2022-08-15
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-22
        相关资源
        最近更新 更多