Game Development Reference
In-Depth Information
The command button
We'll start by defining the
CommandButton
class, but we won't populate it yet; it's just a
placeholder for now until the command bar is complete to manage it. So create a new C#
script in
Assets\Scripts
called
CommandButton
and replace its contents with the
following:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(BoxCollider2D))]
public class CommandButton : MonoBehaviour
{
private CommandBar commandBar;
public InventoryItem Item;
bool selected;
}
Note
Note the additional attributes placed on this class for the
SpriteRender
and
BoxCol-
lider2D
components. This is just Unity's way of stating that these items are mandatory
for this object when used in the scene. If you forget, Unity will warn you.
We have defined a tight reference for the
CommandBar
class that the button is a child of
so that the button knows what is controlling it and they can communicate accordingly.
This enables the button to tell the
CommandBar
class when it is selected, and the
Com-
mandBar
class will be able to clear its selection, if need be.
We also have a property for
InventoryItem
as we are going to use this bar to activate
items for use in the battle (the player's sword), and lastly, a flag to track whether the but-
ton is selected or not.
Tip
As described, we are using tight coupling between the button and the command bar be-
cause of their relationship, as opposed to the loose coupling we did before using mes-
saging. We will still use messaging for some actions, but we will do that later.