Debug.DrawLine not showing in the GameView

删除回忆录丶 提交于 2019-12-07 10:07:02

问题


I'm working on a 2D Unity app and I'm encountering some weird behavior.

This code works just fine.

Debug.DrawLine(button1.transform.position, button2.transform.position, Color.green);

When I run the app, I see a green line in the Scene view.

But nothing appears in the Game view when I have the following line.

Physics2D.Linecast(button1.transform.position, button2.transform.position);

I'm confused as to how Unity is able to draw a line between these two buttons, but for some reason, it's just not doing it in the Game view.

Any idea how I'd troubleshoot this?


回答1:


Just line Serlite said, Physics2D.Linecast is not used to draw a line but to detect if there is an object in the middle of two objects with raycast. It has nothing to do with drawing lines.

As you already know, Debug.DrawLine will only work in the Scene view unless Gizmos is enabled. You can just LineRenderer or the GL functions to draw lines which will work without enabling Gizmos and will also work in a build.

Here is a helper class for drawing line in the Game and Scene view.

public struct LineDrawer
{
    private LineRenderer lineRenderer;
    private float lineSize;

    public LineDrawer(float lineSize = 0.2f)
    {
        GameObject lineObj = new GameObject("LineObj");
        lineRenderer = lineObj.AddComponent<LineRenderer>();
        //Particles/Additive
        lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));

        this.lineSize = lineSize;
    }

    private void init(float lineSize = 0.2f)
    {
        if (lineRenderer == null)
        {
            GameObject lineObj = new GameObject("LineObj");
            lineRenderer = lineObj.AddComponent<LineRenderer>();
            //Particles/Additive
            lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));

            this.lineSize = lineSize;
        }
    }

    //Draws lines through the provided vertices
    public void DrawLineInGameView(Vector3 start, Vector3 end, Color color)
    {
        if (lineRenderer == null)
        {
            init(0.2f);
        }

        //Set color
        lineRenderer.startColor = color;
        lineRenderer.endColor = color;

        //Set width
        lineRenderer.startWidth = lineSize;
        lineRenderer.endWidth = lineSize;

        //Set line count which is 2
        lineRenderer.positionCount = 2;

        //Set the postion of both two lines
        lineRenderer.SetPosition(0, start);
        lineRenderer.SetPosition(1, end);
    }

    public void Destroy()
    {
        if (lineRenderer != null)
        {
            UnityEngine.Object.Destroy(lineRenderer.gameObject);
        }
    }
}

Usage:

LineDrawer lineDrawer;

void Start()
{
    lineDrawer = new LineDrawer();
}

void Update()
{
    lineDrawer.DrawLineInGameView(Vector3.zero, new Vector3(0, 40, 0f), Color.blue);
}

When done, you can call lineDrawer.Destroy();.




回答2:


Debug.DrawLine renders a line gizmo into the Scene View.

If you want a line rendered into the Game View, use a Line Renderer Component.

Line Renderer Docs



来源:https://stackoverflow.com/questions/42819071/debug-drawline-not-showing-in-the-gameview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!