Unity3d Linerenderer 不可见

Unity3d Linerenderer not visible

我正在尝试使用 Linerenderer 在两个 UI 游戏对象之间画一条线。在场景模式下一切正常,但在游戏模式下线是看不见的。我试图改变对象的 Z 位置,但线条仍然不可见。谁能帮我?提前致谢

private LineRenderer lineRenderer;
private float counter;
private float dist;
private Vector3 aPos;
private Vector3 bPos;
public Transform origin;
public Transform destination;
public float lineDrawSpeed = 6f;

// Use this for initialization
void Start()
{
    lineRenderer = GetComponent<LineRenderer>();
    aPos = new Vector3(origin.position.x, origin.position.y, origin.position.z); // Using these to move the lines back
    bPos = new Vector3(destination.position.x, destination.position.y, destination.position.z);

    lineRenderer.SetPosition(0, aPos);
    lineRenderer.SetWidth(3f, 3f);

    dist = Vector3.Distance(origin.position, destination.position);
}

// Update is called once per frame
void Update()
{

    if (counter < dist)
    {
        counter += .1f / lineDrawSpeed;

        float x = Mathf.Lerp(0, dist, counter);

        Vector3 pointA = aPos;
        Vector3 pointB = bPos;

        Vector3 pointAloneLine = x * Vector3.Normalize(pointB - pointA) + pointA;

        lineRenderer.SetPosition(1, pointAloneLine);
    }

}

除非我忽略了您发布的代码中的某些逻辑错误,否则我认为问题可能出在 material。

线条渲染器的一般调试帮助:

尝试设置线条渲染器的color/material:

lineRenderer.sortingOrder = 1;
lineRenderer.material = new Material (Shader.Find ("Sprites/Default"));
lineRenderer.material.color = Color.red; 

如果这不起作用,也许您需要手动指定顶点数?

mineLaser.SetVertexCount (2);

最后,如果这两个都不行,那可能只是逻辑错误;尝试将 lineRenderer 位置的变换设置为某个预定义值,看看它是否显示出来。

对于这个具体问题:

啊,所以它在 canvas 上。假设您的意思是 UI canvas,我相信 linerenderer 是在这种情况下使用的错误工具。查看 this question.

那里的一个答案建议:

just use a panel filled with any color you want and use Height and Width to set the length and the Width of your line

这在"Screen Space - Overlay"Canvas模式下是不可能的。在该模式下,UI 叠加层绘制在场景中的所有内容之上(包括 LineRenderer,实际上是非 UI 元素)。

尝试为您的 Canvas 使用 "Screen Space - Camera" 选项,为您的 Line Renderer 使用 "Use World Space" 选项。

我想你一定是忘了给线条渲染器设置排序层了。因为这可能只是线在场景视图中可见而不是在游戏视图中可见的可能原因。