The unavailable button

We need to correct the following issue: right now, all four elemental buttons are always enabled, which means a power source of fire can be re-switched to the same fire element. This makes no sense.

If the power source element is fire, then the fire elemental button should be visible, but non-interactive. We could simply disable the GameObject itself, but then the button would be invisible. We want it to be grayed out and transparent so that the player knows he/she cannot interact with it right now but still has knowledge of its existence.

This is what we need it to look like:

The unavailable button

Let's see how we can achieve this through code.

The EnableAllButtons() method

First, we need to add a method to our GameManager component, which will enable all buttons. Afterwards, we'll disable the power source's current element button. Open our GameManger.cs script, and add this new EnableAllButtons() method:

// Enable all elemental buttons
public void EnableAllButtons()
{
  // For each elemental button
  foreach(UIButton currentButton in allButtons)
  {
    // Mark it as enabled
    currentButton.isEnabled = true;
    // Update its color
    currentButton.UpdateColor(true);
  }
}

The preceding helper method activates all elemental switch buttons.

The SetButtonState() method

Now, let's add a SetButtonState() method in our GameManager.cs script that will enable or disable elemental UI switch buttons accordingly. Open GameManager.cs, and add the following code for the new SetButtonState() method:

// Used to enable / disable elemental buttons
public void SetButtonState(Elements.Type type, bool state)
{
  // For each button in allButtons[]
  foreach (UIButton currentButton in allButtons)
  {
    // If currentButton is the one to enable/disable
    if (currentButton.name == type.ToString())
    {
      // Change its isEnabled state
      currentButton.isEnabled = state;
      // Update its color now
      currentButton.UpdateColor(true);
      return;
    }
  }
}

The preceding method can be used to disable the power source's element button. The UpdateColor() method updates the button's color with the gray, transparent Disabled color we configured earlier for all our elemental buttons.

Changing button states

Let's implement the buttons' state change. Open our PowerSource.cs script and add these two new lines within the OnPress() method, just above the GameManager.Instance.ShowElementalSwitch(transform); instruction:

// Re-enable all buttons
GameManager.Instance.EnableAllButtons();
// Disable this power source's element button
GameManager.Instance.SetButtonState(type, false);

And that's it! You can now notice that the current element's switch button is disabled, transparent, and grayed out, because that's how we configured our Disabled color when we created our elemental switch UI.

When you switch the power source to a new element, the elemental switch UI buttons are updated accordingly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.176.166