Drawing with GL only appears in scene view, not game view
Hi, I have an extremely simple script attached to the main camera, that uses the GL library to draw random lines (this is just a test) but the lines are not visible from game view/ through the camera. I can see them behaving as expected in the scene view when the game is running. The code is mostly copy and pasted from the unity API reference but I will attach it just in case.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawUI : MonoBehaviour
{
static Material lineMaterial;
static void CreateLineMaterial()
{
if (!lineMaterial)
{
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
}
public void OnRenderObject()
{
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.PushMatrix();
GL.MultMatrix(transform.localToWorldMatrix);
GL.Begin(GL.LINES);
GL.Color(new Color(255, 0, 0));
for (int i = 0; i < 10; i++)
{
GL.Vertex3(0, 0, 0);
GL.Vertex3(Random.Range(-5, 5), Random.Range(-5, 5), Random.Range(-5, 5));
}
GL.End();
GL.PopMatrix();
}
}
As i mentioned this displays correctly in the scene view when running, (random flashing lines originating from camera). I am using Unity 2020, on Ubuntu 20.04, AMD graphics card.
Thanks for any assistance
Comment
Your answer
