Hi guys! Since I decided to postpone the deadline for my master thesis project onto the next session, which takes place in September, I can take some time to focus on minor elements of the project. My aim is to create a little GUI-helper library, that I will use in my future game projects – for example to boost my productivity on game jams. That is also why I’m trying to write scripts that are maximally potentially reusable. The first component that I created was DraggableItem, now it is time for Selectable Items. I needed them for a handy entities creation mechanism, which you can see below.
So, the concept is simple – similar to components named radio buttons. When we activate one, the others get deactivated. Since it is unity, we will probably want to do something when the button gets marked.
One to rule them all
First of all, we want our elements to find their parent. The parent will have a SelectablesContainer script attached that will provide the method to register the child. The container will keep track of its children and will allow them to communicate by exposing an event – ActiveElementChanged.
When a child registers, it obtains its own id and the counter is incremented. When active element is about to change, it calls the ChangeActive method passing its id as an argument. Then, the container sends an event to all selectable items. The ones with non-matching id will get disabled.
The first thing selectable items do while appearing on the scene, is to find the parent container. When it is done, they sign up for the ActiveElementChanged event and request an id.
Selectable items obviously will be selected by clicking on them, so they also need to implement IPointerClickHandler interface. They will have only one public readonly property – IsActive. When it is set, the virtual method OnActiveChanged is called.
It does nothing right now, but is left here for other scripts that will inherit from this class. OnEnable is also virtual, in case future components would need some additional initialization.
This is it, little simple scripts that make entities creation handier. I created also a script that inherits from SelectableItem – ColorSelectableItem, which changes its color when marked. That is when the “core” part ends. Then, my EntityElement could inherit from this class and in the overridden OnActiveChanged method it turns the input field on and off. The only thing left was a handy AttachElementButton – I will need that in a lot more places:
In this script I used the RequireComponent attribute, to ensure that it will be placed only on objects that contain the Button script. This attribute is really useful, because:
• If you already have required component attached to the object it will do nothing,
• If you add this script to a game object that doesn’t have this component attached, it will be added automatically,
• If adding this component would collide with some other (for example – you already have an Image component attached and would want to add a script that requires the Text component) – you will get an error message and the script won’t be added.
I hope that this boring UI part is slowly coming to an end and I will be able to put some spirit into these panels and buttons, but first things first – there’s Easter coming! So, happy Easter!comments powered by Disqus