动态添加的LineRenderer不在游戏视图中显示

时间:2019-10-13 16:09:25

标签: unity3d

我动态添加了一些线条,它们完美地显示在场景视图中,而不是在游戏视图中。 z索引已正确设置,我仅对行使用不同的层。我尝试更改相机的裁剪平面值,在所有物体上都选择了遮罩。

unity screenshot

void Update()
{
    Vector2 mousePos;

    if (Input.GetMouseButtonDown(0))
    {
        Vector3 closeToPoint = IsClickCloseToPoint();

        if (closeToPoint.z != -10000f)
        {
            GameObject newObj = Instantiate(lineGenerator);
            startMousePos = closeToPoint;
            newLine = newObj.GetComponent<LineRenderer>();
            newLine.transform.SetParent(parentObject.transform);
            newLine.positionCount = 2;
        }
    }

    if (Input.GetMouseButton(0))
    {
        if (newLine != null)
        {
            mousePos = Input.mousePosition;
            newLine.SetPosition(0, new Vector3(startMousePos.x, startMousePos.y, -5f));
            newLine.SetPosition(1, new Vector3(mousePos.x, mousePos.y, -5f));
            distance = (mousePos - startMousePos).magnitude;
            distanceText.text = distance.ToString("F2");
        }
    }

    if (Input.GetMouseButtonUp(0))
    {
        if (newLine != null)
        {
            newLine = null;
        }
    }
}

4 个答案:

答案 0 :(得分:1)

您正在画布上绘制所有内容。但是LineRenderer不是UI组件,因此不会显示在画布上。

但是,如果您想使用LineRenderer,请在Unity Answers中检查this info。基本思想是将画布设置为“屏幕空间”并增加线条的宽度。不要忘记分配摄像机。

答案 1 :(得分:0)

我认为您需要在lineRenderer上放置材质。您在场景视图中看到的是游戏视图中未出现的“非材质”纹理:) 我希望有帮助:) 拉夫

答案 2 :(得分:0)

问题在于场景视图和游戏视图中的坐标不同,或者至少您必须针对游戏视图而不是场景视图进行设计。我的台词出现在游戏视图中,但不在画布上。我使用Camera.main.ScreenToWorldPoint(Vektor3)来转换每个vektor。

答案 3 :(得分:0)

我看不到您的LinRenderer的检查器设置,但我怀疑您已经拥有

Alignment设置为Transform Z

  

线面向变换组件的Z轴。

如果您的正交摄影机正好在Z方向上看,它将变得完全不可见。

尝试将Alignment更改为View


还请注意,在屏幕空间覆盖画布的顶部几乎没有显示任何内容。它是要在帧中渲染的last things之一。