TransformGrabbable.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Base class for the visible, interactible transform controls in the ZED MR Calibration scene.
  6. /// Handles the visuals, enabling/disabling objects when grabbed or let go, and knows what grabbed it.
  7. /// Behaviors to actually move the ZED are in inheriting classes.
  8. /// </summary>
  9. public abstract class TransformGrabbable : MonoBehaviour, IXRHoverable, IXRGrabbable
  10. {
  11. /// <summary>
  12. /// Calls SetActive(true) on all objects when a grab starts, and SetActive(false) when it ends.
  13. /// <para>Used to enable/disable the transform and limit indicators.</para>
  14. /// </summary>
  15. [Tooltip("Calls SetActive(true) on all objects when a grab starts, and SetActive(false) when it ends. " +
  16. "Used to enable/disable the transform and limit indicators.")]
  17. public List<GameObject> enableOnlyWhenGrabbed = new List<GameObject>();
  18. /// <summary>
  19. /// Material applied when ZEDXRGrabber hovers over it. If empty, a default one is loaded from Resources.
  20. /// </summary>
  21. [Tooltip("Material applied when ZEDXRGrabber hovers over it. If empty, a default one is loaded from Resources. ")]
  22. public Material hoverMaterial;
  23. private Dictionary<Renderer, Material> baseMaterials = new Dictionary<Renderer, Material>();
  24. protected Transform grabbingTransform;
  25. protected bool isGrabbed
  26. {
  27. get
  28. {
  29. return grabbingTransform != null;
  30. }
  31. }
  32. /// <summary>
  33. /// Make sure we have a hovermat assigned.
  34. /// </summary>
  35. protected virtual void Awake()
  36. {
  37. if (!hoverMaterial)
  38. {
  39. hoverMaterial = Resources.Load<Material>("HoverMat");
  40. }
  41. }
  42. /// <summary>
  43. /// Returns the transform. Exists as the IXRGrabbable and IXRHoverable interfaces can't inherit from Monobehaviours.
  44. /// </summary>
  45. /// <returns></returns>
  46. public Transform GetTransform()
  47. {
  48. return transform;
  49. }
  50. /// <summary>
  51. /// Apply the hover material to all Renderers on and parented to this transform.
  52. /// </summary>
  53. void IXRHoverable.OnHoverStart()
  54. {
  55. foreach (Renderer rend in GetComponentsInChildren<Renderer>())
  56. {
  57. if (!baseMaterials.ContainsKey(rend))
  58. {
  59. baseMaterials.Add(rend, rend.material);
  60. }
  61. rend.material = hoverMaterial;
  62. }
  63. }
  64. /// <summary>
  65. /// Return the original materials to all the Renderers on and parented to this transform.
  66. /// </summary>
  67. void IXRHoverable.OnHoverEnd()
  68. {
  69. if (this == null) return;
  70. foreach (Renderer rend in GetComponentsInChildren<Renderer>())
  71. {
  72. if (baseMaterials.ContainsKey(rend))
  73. {
  74. rend.material = baseMaterials[rend];
  75. }
  76. else Debug.LogError("Starting material for " + rend.gameObject + " wasn't cached before hover started.");
  77. }
  78. }
  79. /// <summary>
  80. /// What happens when ZEDXRGrabber first grabs it. From IXRGrabbable.
  81. /// </summary>
  82. public virtual void OnGrabStart(Transform grabtransform)
  83. {
  84. grabbingTransform = grabtransform;
  85. //Enable all indicators.
  86. foreach (GameObject go in enableOnlyWhenGrabbed)
  87. {
  88. go.SetActive(true);
  89. }
  90. }
  91. /// <summary>
  92. /// What happens when ZEDXRGrabber stops grabbing it. From IXRGrabbable.
  93. /// </summary>
  94. public virtual void OnGrabEnd()
  95. {
  96. grabbingTransform = null;
  97. //Disable all indicators.
  98. foreach (GameObject go in enableOnlyWhenGrabbed)
  99. {
  100. go.SetActive(false);
  101. }
  102. }
  103. }