- Home /
How to render a quad without gameobject?
I'm making a performance-critical grid that involves moving, rotating, and disabling parts of the grid, which is essentially made up of 1x1 quads.
Is there a way I can render these quads with their texture without instantiating a game object for each of them?
Answer by Bunny83 · Apr 28, 2015 at 06:59 PM
Sure there are many ways. It of course depends on what you need exactly. The magic phrase is "procedurally generated content".
The first way is to create a Mesh. Basically you would create a single Mesh which contains all of your quads. Of course for this to work you need to use a texture atlas since the whole mesh will be rendered with one material. So all textures have to be on one texture. If every quad has the same texture it's even better ^^.
Another way is using direct drawing API. Specifically the GUI, GL and Graphics. The downside is usually that each Begin-End pair usually creates a drawcall. So there's no batching happening. I once made this webplayer to explain how FixedUpdate works. The whole project only has one script, one scene which only has one gameobject in it ^^. No other assets. Everything is done inside OnGUI.
If your grid doesn't change too often and not too much, creating a mesh would probably be the best solution. However keep in mind that a single Mesh can only have 65000 vertices. That's a grid of 127x127 quads. IF you need more you have to use multiple meshes.
For anything more specific you need to ask a more specific question ^^. More details would be:
how many "cells" does the grid have?
What texture should be used? a single texture or multiple?
How often should the grid be updated?
How many parts (in % or quad count) of the grid will be updated.
Thanks for this insight. I think I'm going to use a canvas for my grid, but it's great to have this tool in my toolbag.
Answer by screenname_taken · Apr 28, 2015 at 08:17 AM
You need gameobjects to put stuff in. The meshes are components, they don't exist on their own. What you could do is to put alot of those objects in your scene that are either disabled or have their renderer component disabled, have them in a pool/array, and enable what you need. Do what you wish with them and then disable them and put them away someplace that won't mess with anything else. So you don't have the overhead of instantiating and destroying stuff. Stopped doing that some time ago and saved me a lot of spikes.
I remember Aaron Granberg mentioning he didn't make game objects for visualization for his local avoidance demo here: http://arongranberg.com/astar/docs/local-avoidance.php . How did he achieve that effect?
I get a 404 error on the link. Dunno how you could do that, i checked the unity documentation and the script there that modifies meshes in real time through script is calling the mesh filter component.
The link has an extra period on the end. He doesn't say he doesn't have the quads on GameObjects, he says he has them all on the same GameObject (in a single mesh). That's no use if you need to manipulate their transforms individually.
Oops, fixed the link. I don't need to manipulate transforms persay - just visualization for my project (something like in the fixed link). Any ideas now?
Your answer
Follow this Question
Related Questions
Object turns pink when changing material at real time: 1 Answer
Properly stretch texture on quad 1 Answer
How to call Graphics.DrawTexture() in Update 1 Answer
Creating a Masking Object with Variable Pass Through 0 Answers
Texture help 0 Answers