DK GUI API & usage
API & usage of my Unity GUI framework written in C#
I get a lot of questions on building a GUI framework, so I uploaded this to inspire you.
Unfortunatelly the framework isn't currently open source. But if you do your own framework, you should consider this:
- Basically, what you should do is to build components which hold state (properties: x, y, width, height, label, color, alpha... ), and whose Draw() routine does Unity's "OnGUI()" stuff.
- What you need is a base class which is extended by each different component.
- You should have one private Rect property which gets set when changing public properties above, and is used by Draw().
- Composite pattern is the most important here: you should make a container class that has Children collection, and AddChild/RemoveChild methods.
- You build the hierarchy using those methods in Start() routine. You create a control and add it as a child to the container. Then you add this container to some other container and so on. What you get is a tree.
- You should implement invalidation methods for setting properties (basically setting a "dirty" flag on components and recalculating them just before the next update).
- Examine existing open-source frameworks. Google for "Flex invalidation methods". You should look into InvalidateProperties()/CommitProperties() and two other invalidation/validation method pairs.
- Without the use of invalidation your framework is doomed (because of the processing cost).
- Also, your component should extend an EventDispatcher (Flex has a handy one to look into), so you could use event flow in your components.
- Your Stage (top container) should on OnGUI() iterate trough children of each container in the hierarchy and call Draw().
- Your Stage should on Update() iterate through validation methods recursivelly.
- If you do stuff on the container first, and then on children, you get a top-down processing.
- If you process children first and then the container, you get a bottom-up processing (from leaves to root) - this is used for measuring cycle.
Get subscribed to my channel - more to come :-)
Danko
Embedded video:
Sorry, you need to install flash to see this content.
- Login to post comments