ToggleButton.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. /// <summary>
  6. /// 3D button with an on and off state. Meant to be tied to a ToggleGroup3D.
  7. /// Only pressable when not already pressed - relies on ToggleGroup3D to signify when another button has been pressed
  8. /// in order to raise this one. Similar to UI.Toggle and UI.ToggleGroup for the 2D canvas.
  9. /// <para>Used in the ZED MR Calibration scene for the Mode and Hands toggles.</para>
  10. /// <para>See parent class Button3D for hover effects and basic interactions.</para>
  11. /// </summary>
  12. public class ToggleButton : Button3D
  13. {
  14. /// <summary>
  15. /// Toggle group that this button belongs to.
  16. /// </summary>
  17. [Tooltip("Toggle group that this button belongs to.")]
  18. public ToggleGroup3D toggleGroup;
  19. /// <summary>
  20. /// Index of this button within the toggle group. Not set by ToggleGroup3D: should be set manually or via a different script.
  21. /// </summary>
  22. [Tooltip("Index of this button within the toggle group. Not set by ToggleGroup3D: should be set manually or via a different script.")]
  23. public int index;
  24. /// <summary>
  25. /// Events called when this button just became toggled.
  26. /// </summary>
  27. [Tooltip("Events called when this button just became toggled. ")]
  28. [Space(5)]
  29. public UnityEvent OnToggled;
  30. /// <summary>
  31. /// Events called when this button just because un-toggled.
  32. /// </summary>
  33. [Tooltip("Events called when this button just because un-toggled.")]
  34. public UnityEvent OnUnToggled;
  35. /// <summary>
  36. /// What happens when this object is clicked. From IXRClickable.
  37. /// </summary>
  38. /// <param name="clicker"></param>
  39. public override void OnClick(ZEDXRGrabber clicker)
  40. {
  41. RequestStateChange();
  42. }
  43. /// <summary>
  44. /// Tells the applicable ToggleGroup3D that this button was pressed and should be switched to.
  45. /// </summary>
  46. public void RequestStateChange()
  47. {
  48. toggleGroup.ToggleNewButton(index);
  49. }
  50. /// <summary>
  51. /// Set cosmetics and invokes relevant event for a new toggle state. Called by ToggleGroup3D,
  52. /// usually after RequestStateChange is called here.
  53. /// </summary>
  54. public void ChangeToggleState(bool state)
  55. {
  56. if(state == true)
  57. {
  58. col.enabled = false;
  59. brightness = pressedDarkness;
  60. transform.localScale = new Vector3(startScale.x * pressedScaleMult.x, startScale.y * pressedScaleMult.y, startScale.z * pressedScaleMult.z);
  61. OnToggled.Invoke();
  62. }
  63. else //State = false.
  64. {
  65. col.enabled = true;
  66. brightness = unpressedDarkness;
  67. transform.localScale = startScale;
  68. OnUnToggled.Invoke();
  69. }
  70. }
  71. }