ClickButton.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. /// <summary>
  6. /// 3D button that, when clicked by ZEDXRGrabber, fires a single event, and plays an animation to
  7. /// depress it temporarily, like a real button sliding into its slot. Also darkens it slightly.
  8. /// See parent class, Button3D, for more details.
  9. /// </summary>
  10. public class ClickButton : Button3D
  11. {
  12. /// <summary>
  13. /// Called when the button is clicked by ZEDXRGrabber.
  14. /// </summary>
  15. public UnityEvent OnClicked;
  16. [Space(5)]
  17. public float pressedSeconds = 0.2f;
  18. /// <summary>
  19. /// Invoke the event and play the animation that shrinks and darkens the button temporarily.
  20. /// </summary>
  21. /// <param name="clicker"></param>
  22. public override void OnClick(ZEDXRGrabber clicker)
  23. {
  24. OnClicked.Invoke();
  25. StartCoroutine(DisplayClick());
  26. }
  27. /// <summary>
  28. /// Darkens the button slightly and causes it to shrink, as if depressing into its slot.
  29. /// Happens as an animation over time.
  30. /// </summary>
  31. private IEnumerator DisplayClick()
  32. {
  33. col.enabled = false;
  34. brightness = pressedDarkness;
  35. Vector3 scalediff = Vector3.one - pressedScaleMult;
  36. for (float t = 0; t < pressedSeconds / 2f; t += Time.deltaTime)
  37. {
  38. transform.localScale -= scalediff * (Time.deltaTime / (pressedSeconds / 2f));
  39. brightness = Mathf.Lerp(unpressedDarkness, pressedDarkness, t / (pressedSeconds / 2f));
  40. yield return null;
  41. }
  42. for (float t = 0; t < pressedSeconds / 2f; t += Time.deltaTime)
  43. {
  44. transform.localScale += scalediff * (Time.deltaTime / (pressedSeconds / 2f));
  45. brightness = Mathf.Lerp(pressedDarkness, unpressedDarkness, t / (pressedSeconds / 2f));
  46. yield return null;
  47. }
  48. transform.localScale = startScale;
  49. brightness = unpressedDarkness;
  50. col.enabled = true;
  51. brightness = unpressedDarkness;
  52. }
  53. }