Button3D.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Clickable 3D object button that will do something when clicked by a ZEDXRGrabber.
  6. /// Governs only hover-ability, click-ability, and related graphics. Inherited by ClickButton and ToggleButton, which add logic.
  7. /// </summary>
  8. [RequireComponent(typeof(Collider))]
  9. [RequireComponent(typeof(MeshRenderer))]
  10. public abstract class Button3D : MonoBehaviour, IXRHoverable, IXRClickable
  11. {
  12. /// <summary>
  13. /// Material applied when a ZEDXRGrabber hovers over it.
  14. /// </summary>
  15. [Tooltip("Material applied when a ZEDXRGrabber hovers over it. ")]
  16. public Material hoverMaterial;
  17. protected Material buttonMat;
  18. protected MeshRenderer mRenderer;
  19. protected Collider col;
  20. /// <summary>
  21. /// Scale applied to the transform when clicked, to handle its depressed state.
  22. /// <para>This scale is not applied by Button3D itself - only implemented in child classes.</para>
  23. /// </summary>
  24. [Tooltip("Scale applied to the transform when clicked, to handle its depressed state.")]
  25. public Vector3 pressedScaleMult = new Vector3(1, 0.5f, 1);
  26. protected Vector3 startScale;
  27. /// <summary>
  28. /// Brightness setting of the material when not clicked/toggled/etc. Assumes using a shader with a _Brightness property.
  29. /// <para>Not actually set by Button3D; only child classes.</para>
  30. /// </summary>
  31. [Space(5)]
  32. [Tooltip("Brightness setting of the material when not clicked/toggled/etc. Assumes using a shader with a _Brightness property.")]
  33. [Range(0, 1)]
  34. public float unpressedDarkness = 1f;
  35. /// <summary>
  36. /// Brightness setting of the material when clicked/toggled/etc. Assumes using a shader with a _Brightness property.
  37. /// <para>Not actually set by Button3D; only child classes.</para>
  38. /// </summary>
  39. [Tooltip("Brightness setting of the material when clicked/toggled/etc. Assumes using a shader with a _Brightness property.")]
  40. [Range(0, 1)]
  41. public float pressedDarkness = 0.15f;
  42. /// <summary>
  43. /// Value of _Brightness property on attached material.
  44. /// </summary>
  45. protected float brightness
  46. {
  47. get
  48. {
  49. return buttonMat.GetFloat("_Brightness");
  50. }
  51. set
  52. {
  53. buttonMat.SetFloat("_Brightness", value);
  54. }
  55. }
  56. // Use this for initialization
  57. protected virtual void Awake ()
  58. {
  59. mRenderer = GetComponent<MeshRenderer>();
  60. col = GetComponent<Collider>();
  61. col.isTrigger = true;
  62. //Make the button material an instance so we can modify its properties individually.
  63. buttonMat = new Material(mRenderer.material);
  64. mRenderer.material = buttonMat;
  65. if (!hoverMaterial)
  66. {
  67. hoverMaterial = Resources.Load<Material>("HoverMat");
  68. }
  69. startScale = transform.localScale;
  70. }
  71. public Transform GetTransform()
  72. {
  73. return transform;
  74. }
  75. public abstract void OnClick(ZEDXRGrabber clicker);
  76. void IXRHoverable.OnHoverStart()
  77. {
  78. mRenderer.material = hoverMaterial;
  79. }
  80. void IXRHoverable.OnHoverEnd()
  81. {
  82. mRenderer.material = buttonMat;
  83. }
  84. }