ゲームにボタンマネージャーがあるか、またはボタンマネージャーへの参照が必要です。通常、あなたのゲームはbuttonManagerを作成し所有します。
class Game
{
ButtonManager m_buttonManager;
...
}
ボタンマネージャは、OnButtonClickedのようなイベントを公開することができます。
class ButtonManager
{
private Button m_playGameButton;
// delegate to define the type of the event handler
public delegate void ButtonClickedEventHandler(ButtonId buttonId);
// event object matching the type of the event handler
public event ButtonClickedEventHandler OnButtonClicked;
void Update()
{
...
if (m_playGameButton.Clicked)
{
// Launch the event when appropriate if there are any subscribers
if (OnButtonClicked != null)
{
OnButtonClicked(ButtonId.PlayGame)
}
}
}
}
ゲームクラスは、イベントに登録してハンドラメソッドを提供できます。
class Game
{
...
void Initialise()
{
m_buttonManager += ButtonClicked;
}
void ButtonClicked(ButtonId buttonId)
{
switch (buttonId)
{
case ButtonId.PlayGame:
PlayGame();
break;
case ButtonId.OptionsMenu:
OptionsMenu();
break;
}
}
...
}
また、ゲームクラスはボタンマネージャをポーリングできます。
class Game
{
...
void Update()
{
if (m_buttonManager.IsPlayGameButtonHit)
{
PlayGame();
}
else if (m_buttonManager.IsOptionsMenuButtonHit)
{
OptionsMenu();
}
}
...
}
+1:これは私がそれを行う方法であり、値をハードコーディングせずにエンジンを再利用できるということです。 –