0
\$\begingroup\$

I want to draw lines using the Linerenderer only in the game view window.

I'm using Debug.DrawLine but only drawing Red lines but for some reason when the Linerenderer draw a line/s one of the Linerender lines is yellow it seems the green linerenderer is getting on the red one in the scene view window.

 Debug.DrawLine(Input.mousePosition, hit.transform.position, Color.red);

Is there a way to avoid it ? So Debug.DrawLine will draw only in sceneview and the Linerenderer will draw only in the game view window ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateObjects : MonoBehaviour
{
    public GameObject[] objectsToRotate;
    public float speed = 0.1f;
    public Vector3 spinDirection;
    public bool useMouse = false;
    public bool automaticSpin = false;
    public LineRenderer linerendererPrefab;

    List<LineRenderer> lrs = new List<LineRenderer>();

    // Use this for initialization
    void Start()
    {
        for(int i = 0; i < objectsToRotate.Length; i++)
        {
            var lr = Instantiate(linerendererPrefab);
            lrs.Add(lr);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (objectsToRotate.Length > 0)
        {
            for (int i = 0; i < objectsToRotate.Length; i++)
            {
                var hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition), 100.0f);

            for (int x = 0; x < hits.Length; x++)
            {
                RaycastHit hit = hits[x];

                if (hit.collider.name == objectsToRotate[i].name)
                {
                    objectsToRotate[i].transform.Rotate(1, 1, 1);
                    Debug.DrawLine(Input.mousePosition, hit.transform.position, Color.red);
                    SpawnLineGenerator(Input.mousePosition, hit.transform.position, false);
                }
            }

            if(hits.Length == 0)
                SpawnLineGenerator(new Vector3(0,0,0), new Vector3(0,0,0), true);

            if (useMouse == true)
            {
                if (Input.GetMouseButton(0))
                {
                    Rotate(i);
                }
            }
            else
            {
                Rotate(i);
            }
        }
    }

    private void Rotate(int i)
    {
        if (automaticSpin == true)
        {
            objectsToRotate[i].transform.Rotate(1, 1, 1);
        }
    }

    void SpawnLineGenerator(Vector3 start, Vector3 end, bool reset)
    {
        for(int i = 0; i < lrs.Count; i++)
        {
            if (reset)
            {
                lrs[i].SetPosition(0, start);
                lrs[i].SetPosition(1, end);
            }
            else
            {
                lrs[i].SetPosition(0, start);
                lrs[i].SetPosition(1, end);
            }
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

Debug.DrawLine() will only work in Editor mode when Gizmos are activated. this feature won't work in a build.

The LineRenderer component is drawn on runtime, so it works in both editor playmode and in the builds. So you can rest, your debug.drawlines won't show up in your executable.

Now, if you want to just use lines for debugging purposes in build mode, you can use openGL methods. here is a snippet to draw lines with GL.

/// <summary>
    ///  Renders a Line using OpenGL
    /// GL.Vertex3 are followed up, if we declare 4 vertices it will draw two lines, one between A and B and one between C and D
    /// </summary>
    /// <param name="mat"></param>
    /// <param name="startPos"></param>
    /// <param name="endPos"></param>
    /// <param name="color"></param>
    public static void OnLineRender(Material mat, Vector3 startPos, Vector3 endPos, Color color)
    {
        GL.PushMatrix();
        GL.LoadProjectionMatrix(Camera.main.projectionMatrix);
        GL.modelview = Camera.main.worldToCameraMatrix;

        mat.SetPass(0);

        GL.Begin(GL.LINES);
        GL.Color(color);
        GL.Vertex3(startPos.x, startPos.y, startPos.z);
        GL.Vertex3(endPos.x, endPos.y, endPos.z);
        GL.End();

        GL.PopMatrix();
    }

Ensure to call this methods from an OnPostRender() method in a script attached to your mainCamera. or if you use the new ScriptableRenderPipeline, you will have to call it this way:

private void InitGL()
    {
        // Creates a Material
        _lineMat = new Material(Shader.Find("Hidden/Internal-Colored"));
        _lineMat.hideFlags = HideFlags.None;
        // Turn on alpha blending
        _lineMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
        _lineMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
        // Turn backface culling off
        _lineMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
        // Turn off depth writes
        _lineMat.SetInt("_ZWrite", 1);
        RenderPipeline.BeginCameraRendering(Camera.main);
        RenderPipeline.beginCameraRendering += OnPostRender;
    }

    public void OnPostRender(Camera cam)
    {
        // do Stuff Here
        OnLineRender(_lineMat, start, end, Color.red)
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.