Game Development Reference
In-Depth Information
Here, we simply get the SpriteRenderer component or the button itself (not its child
item) and assign the correct button sprite, either the selected or unselected image con-
figured for the command bar.
Next, we add the ClearSelection function:
public void ClearSelection()
{
selected = false;
UpdateSelection();
}
This simply sets the selected state to false and then calls the UpdateSelection
function to set the button sprite appropriately.
Finally, we add the OnMouseDown function to react to the BoxCollider2D compon-
ent we have on the button and receive clicked events:
void OnMouseDown()
{
if (commandBar.CanSelectButton)
{
selected = !selected;
UpdateSelection();
commandBar.Selectbutton(selected ? this : null);
}
}
So now when the user clicks, it will flip the selected state for the button and then call the
UpdateSelection function to set the correct button sprite. You will note that this
function depends on a property in the CommandBar script that doesn't exist yet. This is
there so that if for some reason the command bar is disabled or input is not allowed, then
we can ignore any clicks; we'll add this shortly.
Once the user has selected the button, we tell the command bar about it. We then let it
manage what happens to the selection after that. We could handle it within the button it-
self, but this would mean lots of complicated code in this very simple class; it's best to
Search WWH ::




Custom Search