EDIT: Look like someone did it before, more optimized than my script and with more functions http://pastebin.com/69QP1s45 author: artemisart Hello, I'm currently working on some external ballistic stuff, and Debug.DrawRay and Debug.DrawLine were VERY useful. I've made some research to get these lines into the game for whatever reason, and I've figured that using the GL library was the way to do it. But impossible to find scripts to do it simple. So I've made these 2 scripts: Code (csharp): /* HOW TO USE, Put DrawLineGLViewer.js on the cameras you want the lines to be rendered THIS SCRIPT SHOULD BE IN YOUR PLUGINS FOLDER DrawLineGL.DrawLine(StartPos:Vector3, EndPos:Vector3, Color:Color, LifeTime:float); LifeTime is optional, if none then it'll be rendered 1 frame */ static var lineMaterial : Material; static var linesGL:lineGL[]; class lineGL { var lt: float; var st: Vector3; var ed: Vector3; var c: Color; function lineGL(nst:Vector3, ned:Vector3, nc:Color, nlt:float) { lt = Time.time + nlt; st = nst; ed = ned; c = nc; } } static function CreateLineMaterial() { if( !lineMaterial ) { lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" + "SubShader { Pass { " + " Blend SrcAlpha OneMinusSrcAlpha " + " ZWrite Off Cull Off Fog { Mode Off } " + " BindChannels {" + " Bind \"vertex\", vertex Bind \"color\", color }" + "} } }" ); lineMaterial.hideFlags = HideFlags.HideAndDontSave; lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave; } } static function CreateLinesGL() { if(!linesGL) { linesGL = new lineGL[0]; } } static public function DrawLine(st:Vector3, ed:Vector3, c:Color, lt:float) { var nlGL : lineGL = new lineGL(st,ed,c,lt); var nlinesGL = new lineGL[linesGL.length + 1]; for(var i : int = 0; i<nlinesGL.length;i++) { if(i<linesGL.length) { nlinesGL[i] = linesGL[i]; }else { nlinesGL[i] = nlGL; } } linesGL = nlinesGL; } static public function DrawLine(st:Vector3, ed:Vector3, c:Color) { var nlGL : lineGL = new lineGL(st,ed,c,0); var nlinesGL = new lineGL[linesGL.length + 1]; for(var i : int = 0; i<nlinesGL.length;i++) { if(i<linesGL.length) { nlinesGL[i] = linesGL[i]; }else { nlinesGL[i] = nlGL; } } linesGL = nlinesGL; } static public function ResizeLines(index:float) { var nlinesGL = new lineGL[linesGL.length - 1]; for(var i : int = 0; i<linesGL.length; i++) { if(i < index) { nlinesGL[i] = linesGL[i]; }else if(i < nlinesGL.length) { nlinesGL[i] = linesGL[i+1]; } } linesGL = nlinesGL; } Code (csharp): #pragma strict function Awake() { DrawLineGL.CreateLinesGL(); } function OnPostRender() { DrawLineGL.CreateLineMaterial(); // set the current material DrawLineGL.lineMaterial.SetPass( 0 ); GL.Begin( GL.LINES ); if(DrawLineGL.linesGL.Length > 0) { for(var i : int = 0; i<DrawLineGL.linesGL.length; i++) { GL.Color (DrawLineGL.linesGL[i].c); GL.Vertex3 (DrawLineGL.linesGL[i].st.x,DrawLineGL.linesGL[i].st.y,DrawLineGL.linesGL[i].st.z); GL.Vertex3 (DrawLineGL.linesGL[i].ed.x,DrawLineGL.linesGL[i].ed.y,DrawLineGL.linesGL[i].ed.z); if(Time.time > DrawLineGL.linesGL[i].lt) { DrawLineGL.ResizeLines(i); } } } GL.End(); } So put the first script on your Plugins folder, and the second script on whatever camera you will use. Then just use DrawLineGL.DrawLine(StartPosition:Vector3, EndPosition:Vector3, Color:Color, LifeTime:Float); Code (csharp): DrawLineGL.DrawLine(this.transform.position, target.transform.position, Color.Blue,2); DrawLineGL.DrawLine(this.transform.position, target.transform.position + , Color.Blue);//If no lifetime line is rendered 1 frame Dunno if it'll be useful, (maybe someone made something like that but I didn't find it so...), just tell me if it was useful for you Thanks, Sylvain
Thanks, I've requested an account on unifywiki to post it, and I'll upload it to the asset store asap .
Hello, maybe you'll be interested by my script which does quite the same thing : http://pastebin.com/69QP1s45 (but with more functions, it's optimized as well), you can also find it here : http://www.unity3d-france.com/unity/phpBB3/viewtopic.php?f=24&t=5409 (in french).
Nice! I couldn't find it when I needed it! You should spread it everywhere o/ [Effectivement je pense jamais à chercher côté fr]
Needed to draw simple circle with artemisart script, added this code: (circle part taken from some unity answers post) Code (CSharp): public static void DrawCircle (Vector3 center, float radius, Color? color = null, float duration = 0, bool depthTest = false) { //float degRad = Mathf.PI / 180; for(float theta = 0.0f; theta < (2*Mathf.PI); theta += 0.2f) { Vector3 ci = (new Vector3(Mathf.Cos(theta) * radius + center.x, Mathf.Sin(theta) * radius + center.y, center.z)); DrawLine (ci, ci+new Vector3(0,0.02f,0), color, duration, depthTest); } }
Thanks a lot for this info!!! I'm trying to implement it, but I'm getting some errors calling GL_Line.DrawLine (startLine, endLine, myColor ,0.0f,true); "cannot be accessed with an instance reference, qualify it with a type name instead" Do you have any example on how to use it? Thnaaaks!
If you use this script http://pastebin.com/69QP1s45 then you should call, for instance : GLDebug.DrawLine(new Vector3(0, 0, 0), new Vector3(1, 1, 1), Color.red, 1);
Unfortunately, yes. This code was made for Unity 3.x (and compatible with 4.x if i remember correctly). The error comes from this script ? or the one that use it ?
Hi Actually I don't have errors right now. But it only paint the lines on the Gizmos or in the editor window, but nothing on the scene. What could cause this? Thanks for the help
Sorry, I forgot to say that the script has to be on a camera for OnPostRender to be called. Can you retry with the script attached to the GameObject of your main camera ?
These line draw shaders look nice and crisp in Unity 4 if DX11 is turned off, but with DX11 active and in Unity 5 they are fuzzy. Any shader wizards that have any idea how to make a line shader that draws correctly under DX11?
Hey, im using apex path and i want to show this debugging path in game view. have tried with a linerenderer but still not working how to enable gizmo drawing in game? thanks Code (CSharp): /* Copyright © 2014 Apex Software. All rights reserved. */ namespace Apex.Debugging { //using Apex; using Apex.PathFinding; using Apex.Steering; using Apex.Steering.Components; using Apex.Units; using UnityEngine; //using System; using System.Collections.Generic; /// <summary> /// //List<Vector3> posSpheres = new List<Vector3>(); /// Visualization component that draws gizmos to represent a moving unit's current path. /// </summary> [AddComponentMenu("Apex/Unit/Debugging/Path Visualizer", 1024)] [ApexComponent("Debugging")] public partial class PathVisualizer : Visualizer //public partial class PathVisualizer : MonoBehaviour { public List<Vector3> posSpheres = new List<Vector3>(); /// <summary> /// The route color /// </summary> public Color routeColor = new Color(148f / 255f, 214f / 255f, 53f / 255f); /// <summary> /// The way point color /// </summary> public Color waypointColor = new Color(0f, 150f / 255f, 211f / 255f); /// <summary> /// Whether to show segment markers /// </summary> public bool showSegmentMarkers = false; private IUnitFacade _unit; //public IUnitFacade _unit; /// <summary> /// Called on start /// </summary> protected override void Start() //void Start() { _unit = this.GetUnitFacade(); } void Update() { LineRenderer lineRenderer = GetComponent<LineRenderer>(); // Also, what is the value of lengthOfLineRenderer? Ensure it is more than 1. var points = new Vector3[lengthOfLineRenderer]; var t = Time.time; for (int i = 0; i < lengthOfLineRenderer; i++) { points[i] = new Vector3(i * 0.5f, Mathf.Sin(i + t), 0.0f); } lineRenderer.positionCount = points.Length; lineRenderer.SetPositions(points); } /// <summary> /// Partial method for draw visualization. /// </summary> partial void DrawVisualizationPartial(); /// <summary> /// Draws the actual visualization. /// </summary> protected override void DrawVisualization() { DrawVisualizationPartial(); if (_unit == null || !_unit.isMovable) { return; } Vector3 heightAdj = new Vector3(0.0f, 0.2f, 0.0f); Gizmos.color = this.routeColor; var prev = _unit.position; if (_unit.nextNodePosition.HasValue) { prev = _unit.nextNodePosition.Value; Gizmos.DrawLine(_unit.position + heightAdj, prev + heightAdj); //VectorLine.SetLine (_unit.position + heightAdj, prev + heightAdj); //VectorLine.SetLine (Color.yellow, Vector3(0, 0, 0), Vector3(100, 200, 0)); } if (_unit.currentPath != null) { foreach (var n in _unit.currentPath) { if (n is IPortalNode) { continue; } if (showSegmentMarkers) { Gizmos.DrawSphere(prev, 0.2f); } Gizmos.DrawLine(prev + heightAdj, n.position + heightAdj); prev = n.position; } } Gizmos.color = this.waypointColor; if (_unit.currentWaypoints != null) { heightAdj.y = 1.0f; foreach (var wp in _unit.currentWaypoints) { var pinHead = wp + heightAdj; Gizmos.DrawLine(wp, pinHead); Gizmos.DrawSphere(pinHead, 0.3f); posSpheres.Add(pinHead); } } } } }