首页 文章

Unity3d Linerenderer不可见

提问于
浏览
3

我试图用Linerenderer在两个UI GameObjects之间绘制一条线 . 在场景模式中,一切正常,但在游戏模式中,线条是不可见的 . 我试图改变对象的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);
    }

}

3 回答

  • 0

    除非我忽略了你发布的代码中的一些逻辑错误,否则我认为问题可能出在材料上 .

    Generic debugging help for line renderers:

    尝试设置线条渲染器的颜色/材质:

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

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

    mineLaser.SetVertexCount (2);
    

    最后,如果这两者都不起作用,那可能只是一个逻辑错误;尝试将 lineRenderer 位置的变换设置为某个预定义值并查看它是否显示 .

    For this specific question:

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

    其中一个答案表明:

    只需使用填充了所需颜色的面板,然后使用“高度”和“宽度”设置线条的长度和宽度

  • 1

    这在“屏幕空间 - 叠加”画布模式下是不可能的 . 在该模式下,UI叠加层绘制在Scene中的所有内容之上(包括LineRenderer,实际上是非UI元素) .

    尝试为Canvas使用“Screen Space - Camera”选项,为Line Renderer使用“Use World Space”选项 .

  • 0

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

相关问题