Unity linerenderer错误

时间:2016-09-07 20:07:23

标签: c# unity3d drawing paint

我在Unity制作绘图应用程序,我正在使用线条渲染器。这是代码,问题是每当我用另一种颜色创建新线时,它会在第一种颜色的后面绘制。有人可以帮帮我吗?我只是不知道如何解决。

我正在考虑在切换颜色时向线渲染器添加更多z位置,但我不确定它是否会起作用。

    public GameObject lineDrawPrefabs;
    public Material black;
    public Material green1;
    public Material blue;
    public Material pink;
    public Material orange;
    public Material brown;
    public Material green2;
    public Material blue2;
    public Material yellow;
    public Material red;
    public Material white;

    public static string nowcolor;
    private bool isMousePressed;
    private GameObject lineDrawPrefab;
    private LineRenderer lineRenderer;
    List<Vector3> drawPoints = new List<Vector3>();

   // public static bool pencil;



    void Start()
    {

        isMousePressed = false;     
    }


    void Update()
    {
      //  if (Input.GetMouseButtonDown(1))
      //  {
       //     // delete the LineRenderers when right mouse down
       //     GameObject[] delete = GameObject.FindGameObjectsWithTag("LineDraw");
        //    int deleteCount = delete.Length;
        //    for (int i = deleteCount - 1; i >= 0; i--)
        //        Destroy(delete[i]);
      //  }

        if (Input.GetMouseButtonDown(0))
        {
            // left mouse down, make a new line renderer
            isMousePressed = true;
            lineDrawPrefab = GameObject.Instantiate(lineDrawPrefabs) as GameObject;
            lineRenderer = lineDrawPrefab.GetComponent<LineRenderer>();
            CheckColor();
            lineRenderer.SetVertexCount(0);
        }
        else if (Input.GetMouseButtonUp(0))
        {
            // left mouse up, stop drawing
            isMousePressed = false;
            drawPoints.Clear();
        }

        if (isMousePressed)
        {
            // when the left mouse button pressed
            // continue to add vertex to line renderer
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mousePos.z = -1000;
            if (!drawPoints.Contains(mousePos))
            {
                drawPoints.Add(mousePos);
                lineRenderer.SetVertexCount(drawPoints.Count);
                lineRenderer.SetPosition(drawPoints.Count - 1, mousePos);
            }
        }
    }

    public static void deletedrawing() {
        //     // delete the LineRenderers when right mouse down
           GameObject[] delete = GameObject.FindGameObjectsWithTag("LineDraw");
           int deleteCount = delete.Length;
           for (int i = deleteCount - 1; i >= 0; i--)
           Destroy(delete[i]);
    }

Here is the image,first time I used the black color,then the green

1 个答案:

答案 0 :(得分:0)

我修复了这个问题,我只是在图层中使用了顺序,每次用户选择颜色时都添加了+1,现在它工作正常

将此添加到主脚本

 public static int orderinlayer = 1;

并添加了lineRenderer.sortingOrder = orderinlayer;这里

 if (Input.GetMouseButtonDown(0))
        {
            isMousePressed = true;
            lineDrawPrefab = GameObject.Instantiate(lineDrawPrefabs) as GameObject;
            lineRenderer = lineDrawPrefab.GetComponent<LineRenderer>();
            lineRenderer.sortingOrder = orderinlayer;
            CheckColor();
            lineRenderer.SetVertexCount(0);
        }

从另一个脚本中我刚刚添加的颜色选择器是什么

ScriptLineGameobject.orderinlayer += 1;

每个color.Now工作!