Unity Draw Grid Lines on Plane
I ended up asking over at Unity Answers, and I was quickly given a reliable answer from Bunny83, including a working example of a custom inspector with custom drawing logic. Ultimately, you lot can describe lines over a custom inspector using OpenGL, I was just doing information technology incorrect. Of detail note, the answer I received made note of boosted requirements I had missed:
- I had to reserve a infinite to depict over, using
GUILayoutUtility.GetRect()
. - I had to ensure I was only cartoon during the
EventType.Repaint
issue. - While not a requirement, I had to assign a specific material, using
material.SetPass()
. Not doing this can have undesirable results. - I had to utilize further common OpenGL logic, such as clearing a new area with
GL.Clear()
, and pushing and popping the current matrix withGL.PushMatrix()
andGL.PopMatrix()
.
<summary>The material to employ when cartoon with OpenGL.</summary> private Fabric material; void OnEnable() { // Find the "Subconscious/Internal-Colored" shader, and enshroud information technology for use. material = new Fabric(Shader.Find("Hidden/Internal-Colored")); } public override void OnInspectorGUI() { // Begin to draw a horizontal layout, using the helpBox EditorStyle GUILayout.BeginHorizontal(EditorStyles.helpBox); // Reserve GUI infinite with a width from 10 to 10000, and a stock-still height of 200, and // cache it equally a rectangle. Rect layoutRectangle = GUILayoutUtility.GetRect(x,10000,200,200); if(Event.electric current.type == EventType.Repaint) { // If we are currently in the Repaint event, brainstorm to depict a prune of the size of // previously reserved rectangle, and button the electric current matrix for drawing. GUI.BeginClip(layoutRectangle); GL.PushMatrix(); // Articulate the current render buffer, setting a new background colour, and prepare our // textile for rendering. GL.Clear(true, simulated, Color.black); cloth.SetPass(0); // Beginning drawing in OpenGL Quads, to describe the background sheet. Set the // color blackness every bit the current OpenGL cartoon colour, and draw a quad covering // the dimensions of the layoutRectangle. GL.Begin(GL.QUADS); GL.Color(Colour.black); GL.Vertex3(0, 0, 0); GL.Vertex3(layoutRectangle.width, 0, 0); GL.Vertex3(layoutRectangle.width, layoutRectangle.elevation, 0); GL.Vertex3(0, layoutRectangle.height, 0); GL.End(); // Start drawing in OpenGL Lines, to depict the lines of the filigree. GL.Begin(GL.LINES); // Store measurement values to determine the offset, for scrolling animation, // and the line count, for drawing the grid. int offset = (Fourth dimension.frameCount * 2) % l; int count = (int)(layoutRectangle.width / 10) + 20; for(int i = 0; i < count; i++) { // For every line being drawn in the grid, create a colour placeholder; if the // electric current alphabetize is divisible by v, nosotros are at a major segment line; set this // color to a dark greyness. If the current index is non divisible by 5, nosotros are // at a minor segment line; set this colour to a lighter grey. Set the derived // color as the current OpenGL drawing colour. Colour lineColour = (i % 5 == 0 ? new Color(0.5f, 0.5f, 0.5f) : new Color(0.2f, 0.2f, 0.2f); GL.Color(lineColour); // Derive a new x according from the initial alphabetize, converting it straight // into line positions, and move it back to adapt for the animation offset. float x = i * 10 - first; if (x >= 0 && x < layoutRectangle.width) { // If the current derived x position is within the premises of the // rectangle, draw another vertical line. GL.Vertex3(10, 0, 0); GL.Vertex3(x, layoutRectangle.height, 0); } if (i < layoutRectangle.acme / 10) { // Convert the current index value into a y position, and if it is within // the premises of the rectangle, depict some other horizontal line. GL.Vertex3(0, i * x, 0); GL.Vertex3(layoutRectangle.width, i * 10, 0); } } // End lines cartoon. GL.End(); // Pop the electric current matrix for rendering, and end the drawing clip. GL.PopMatrix(); GUI.EndClip(); } // End our horizontal GUILayout.EndHorizontal(); }
Source: https://gamedev.stackexchange.com/questions/141302/how-do-i-draw-lines-to-a-custom-inspector
Postar um comentário for "Unity Draw Grid Lines on Plane"