- Home /
Correct way to draw on screen for 2D game
I have rectangular sprites connected to each other to form a rope-like object. When these objects collide with other objects and rotate, gaps appear between the rectangles. I would like these to appear as a contiguous rope with no gaps. In other 2D game engines, I would just draw lines between the necessary points and fill in the center of the shape. I am new to Unity and am wondering what is the appropriate way to do this.
Should I draw on the screen using OnGUI?
Should I create sprites at run-time and fill in the texture programmatically, then rotate/move the sprite as the rope requires? This seems sort of difficult to me initially.
Should I create some kind of custom mesh object where I define the vertices and just fill in the texture programmatically? I'm not even sure how possible this is. Would Graphics.DrawMesh() work for this?
Is Unity maybe not the game engine that I should be using for this type of game that requires a lot of custom drawing on the screen when the drawing is not related to the GUI?
Thanks in advance and let me know if more clarification is needed.
Answer by robertbu · Dec 22, 2013 at 10:31 PM
If you don't mind spending a bit of money, Vectrosity is a great line drawing package. This is an efficient line drawing solution.
There are a number of other ways to draw lines:
[http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/] [2]
In addition, you could size and scale quads as lines. The code to do this has been posted a few times in this last year. Here is one post.
http://answers.unity3d.com/questions/444478/instantiating-platforms-between-two-vectors.html
[2]: http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/
Thank you. This information helps. After looking at my question, I don't think I was 100% clear enough with what I wanted to do. This information about drawing just lines will come in handy for other things I want to do.
But, what should I do if I want to outline an arbitrary polygon and fill it in with a solid color? I'm also interested to know if there is a good method for filling the shape in with a non-solid texture, but my immediate concern would just require a solid color.
Typically this problem is solved by creating or manipulating a mesh. If the polygon is convex, then constructing the mesh is not that difficult. If it is not convex, take a look at this link:
http://wiki.unity3d.com/index.php?title=Triangulator
Outlining can either be done by drawing lines, or by shader. Take a look at the Wiki shader archive and search for 'outline':
http://wiki.unity3d.com/index.php?title=Shader_Archive
The what and how of transparency is handled by the shader associated with the material used on the texture. The typical approach uses the $$anonymous$$aterial.color to set the alpha, but if you have many of the same objects using the same (initial) material, then vertex colors may be a better (from a performance standpoint) way to create transparency.
Great. That looks like exactly what I need. I'll give it a try. Thanks!
hello, did anyone get an answer for the same .. I want to draw a polygon on a texture and fill it to make it transparent... Can you please help
Your answer
Follow this Question
Related Questions
create line on a texture 2 Answers
How can I draw vectors onto a Texture2D? 0 Answers
Draw triangles on texture 1 Answer
Sweeping trough a 2D Color array 2 Answers
Drawing an arrow on a circle 1 Answer