InstructionsButton.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Google.Maps.Examples.Shared {
  4. /// <summary>
  5. /// Script for hiding this <see cref="GameObject"/> at the press of the required
  6. /// <see cref="Button"/>.
  7. /// </summary>
  8. [RequireComponent(typeof(Button))]
  9. public sealed class InstructionsButton : MonoBehaviour {
  10. [Tooltip(
  11. "Optional Slider to extend upwards to fill the empty space left by this Button when it " +
  12. "is hidden.")]
  13. public Slider Slider;
  14. /// <summary>
  15. /// Connect <see cref="Button"/> press to hiding of this <see cref="GameObject"/>.
  16. /// </summary>
  17. private void Start() {
  18. // Get required Button, and connect to hiding of this GameObject (and optionally extending
  19. // given Slider).
  20. Button button = GetComponent<Button>();
  21. button.onClick.AddListener(() => {
  22. // Make this Button non-interactive so it cannot be clicked again.
  23. button.interactable = false;
  24. // See if a Slider component has been given as a parameter.
  25. if (Slider != null) {
  26. // Adjust Slider's height by the height of this Button's displayed image, so that as we
  27. // hide the Button, the Slider's height is extended over the empty space left by the
  28. // Button.
  29. float buttonHeight = button.targetGraphic.rectTransform.rect.height;
  30. RectTransform sliderRect = Slider.GetComponent<RectTransform>();
  31. sliderRect.offsetMax = sliderRect.offsetMax + Vector2.up * buttonHeight;
  32. }
  33. // Hide this Button.
  34. gameObject.SetActive(false);
  35. });
  36. }
  37. }
  38. }