Draw line in a compiled game?

I need something like Debug.DrawLine which i can use ingame i dont want to use OnDrawGizmos because it would be a messy work get it working. Any ideas?

Search the forum!

That thread is 10 days old and just as redundant as this one :wink:

i am sorry for not using the search function :sweat_smile:
but i was hoping for an easier way because i am really out of time but thank you :slight_smile:

It’s just a pet peeve with us, as we see it a lot and we have to keep answering posts that have been answered several times over already. :wink:

-Jeremy

FAQ! FAQ!

(Yeah, I know UT has said they’ve got that on the radar. :slight_smile: Just thought I’d poke a bit…)

Anyway, using GL.LINES is pretty easy; at least one of those links has code examples. Requires Pro though.

–Eric

In all fairness the search functionality of the forums are not very good. Hence there are even post about using google to search it…

Tom and I are starting to piece it together… my concern is if people will actually check the FAQ before they post. A rule of thumb should be if you’ve been using Unity for less than 3 months and run into a problem chances are someone else has also run into the same problem and posted about it so just do a quick search first (and admittedly a Google search is much better).

Ethan

@script RequireComponent(Camera)

static var lineMaterial : Material;
var numberOfPoints = 400;
private var points : Vector3[];
var rotateSpeed = 10.0;
var amount = .02;
var extent = 15.0;
var sineNumber = .025;
var sineAddSpeed = .05;
var moveAmount = .02;

function Start () {
   transform.position = Vector3.forward*-20;
   points = new Vector3[numberOfPoints];
   var sineCount = 0.0;
   for (i = 0; i < numberOfPoints; i++) {
      points[i] = Vector3(Mathf.Lerp(-extent, extent, Mathf.InverseLerp(0, numberOfPoints, i)), Mathf.Sin(sineCount), 0.0);
      sineCount += sineNumber;
   }
   CreateLineMaterial();
}

function Update () {
   transform.Rotate(Vector3.forward*Time.deltaTime*rotateSpeed);
   transform.Translate(Vector3.forward*Mathf.Sin(Time.time*2.0)*moveAmount);
   
   for (i = 0; i < numberOfPoints; i++) {
      points[i].y += Mathf.Sin(Time.time+sineNumber*i)*amount;
      points[i].z += Mathf.Cos(Time.time+sineNumber*i)*amount;
   }
   sineNumber += Input.GetAxis("Vertical") * sineAddSpeed * Time.deltaTime;
   if (Input.GetKeyDown("r")) {Application.LoadLevel(0);}
}

function CreateLineMaterial () {
   lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
      "SubShader { Pass {" +
      "   BindChannels { Bind \"Color\",color }" +
      "   Blend SrcAlpha OneMinusSrcAlpha" +
      "   ZWrite Off Cull Off Fog { Mode Off }" +
      "} } }");
   lineMaterial.hideFlags = HideFlags.HideAndDontSave;
   lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}

function OnPostRender () {
   lineMaterial.SetPass(0);
   GL.Begin(GL.LINES);
   GL.Color(Color.yellow);
   for (i = 0; i < numberOfPoints-1; i++) {
      GL.Vertex3(points[i].x, points[i].y, points[i].z);
      GL.Vertex3(points[i+1].x, points[i+1].y, points[i+1].z);
   }
   GL.End();
}

i am not really able to understand it all but i think the onpostrender is called everyframe and this gl.Vertex3 converts a 3D vector to screenpixels and draws the line between the first gl.vertex3 and the next one right?
and at the beginning i have to create the material.

So the GL stuff is like immediate mode OpenGL. Because of that, it is getting called OnPostRender(). Otherwise your GL calls are potentially going to be cleared by the camera. As for “converts a 3D vector to screenpixels” that doesn’t sound like the right phrasing, but I think you are getting the idea – GL.Vertex3 represents the point in your scene (x, y, z) where one of the two end vertices for this line are going to be. If I recall, the reason you are creating the material each time is because the GL stuff is going to be using whatever the most recent material is, and you want to make sure that it is the right one.

You will notice in your script that it should be attached to a camera. Also keep in mind that this is only going to work with Pro.

thank you but i found out i cant use it =(
so i want to know how to create prefabs (see new thread) :cry: :frowning:

Yeah, no matter how prominent it is, there will always be a few who somehow don’t see it. But at least if it’s there, all you have to do is say “check the FAQ, dummy!” Maybe without the “dummy” part though. :wink:

–Eric