- Home /
OnGui elements as objects?
Apologies if this was already answered somewhere, I didn't seem to be able to find it.
So basically what I'm asking is if someone can direct me in the right way, because I cant seem to find the answer to this one myself.
Long story short, when writing code for gameplay mechanics, you write your class, attach it to a game object, and it is there as a component on that object which I can grab.
In editor scripting however is where I need to change my mindset.
Example, I create my own editor window, in it I can draw some buttons, input fields, etc via OnGui. But, say I create a box Gui.Box(some rect and other parameters). And now I want for that box to have some fields on it, in a way as object has components.
Basically can I declare a gui box within an editor window in a similar way I can declare a GameObject as a variable, and then search trough its components for different values?
A bit raw question I know, all tutorials and documentation I found go trough setting up input fields, buttons, etc in editor window, but this kind of thing I can't find.
Just in case I didn't explain it well, most similar thing to what I'm describing would be a node in a node editor, a "box" with its values as one object from which I can send or grab data when needed.
Again, I don't expect someone typing me a lesson here, I just need a pointer in the right direction, like some kind of "basics" tutorial regarding "instantiating" elements, and since I never tried editor scripting before, if this makes no sense, someone let me know.
Thanks in advance :)
Answer by Bunny83 · Dec 27, 2018 at 09:26 PM
No, Editor GUI scripting uses the IMGUI system (immediate mode GUI). With this system the GUI controls aren't represented by objects but each time OnGUI is called all elements that should be visible has to be redrawn.
I've once written this GUI crash course for the IMGUI system which was back then also the runtime GUI system. If you have trouble reading the post because UA messed up the markdown renderer some time ago, you can find a copy of the post on my github page which does proper markdown rendering.
Of course you can wrap certain things inside your own custom classes which could store the state of your GUI elements and have some sort of "Render" method which you call from inside OnGUI. It's up to you how you handle your GUI stuff. Most of the Unity editor GUI is actually written in managed code using the IMGUI system. More complex things are often outsourced into seperate classes. Unity provided the source code of most of their editor GUI code for reference on github. Please note that the code they provided can not be used in any project since it's just for reference. Make sure you read the licence that is linked in the readme of the github repository
If you have more questions you may want to ask a seperate question (maybe in the help room) and be more concrete what you want to do where you're stuck and what you have tried so far
Answer by CapSovaa · Dec 28, 2018 at 11:38 AM
A lot of material to chew trough there, excellent. I believe this crash course of yours is exactly what I need.
About having more questions, I have tons, but I'll go trough what you sent me here first. Like you said there, no point to ask how to build a car from scratch.
Anyway, thank you, much appreciated, just what I needed :)
Best regards.