PrimaryHandSwitcher.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Enables/disables objects and calls events to switch controller-mounted objects so that
  6. /// ones intended for the dominant hand are over the user's preferred hand, and vice-versa with
  7. /// objects meant for the non-dominant hand.
  8. /// Used in the ZED MR Calibration scene to switch the hand-mounted menu between hands.
  9. /// </summary>
  10. public class PrimaryHandSwitcher : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// Left controller transform.
  14. /// </summary>
  15. [Tooltip("Left controller transform.")]
  16. public Transform leftHand;
  17. /// <summary>
  18. /// Right controller transform.
  19. /// </summary>
  20. [Tooltip("Right controller transform.")]
  21. public Transform rightHand;
  22. private static GameObject _primaryhandobject;
  23. /// <summary>
  24. /// GameObject currently designated as the primary hand.
  25. /// </summary>
  26. public static GameObject primaryHandObject
  27. {
  28. get
  29. {
  30. if (!_primaryhandobject)
  31. {
  32. ZEDXRGrabber primarygrabber = FindObjectOfType<ZEDXRGrabber>();
  33. _primaryhandobject = primarygrabber.transform.parent.gameObject;
  34. }
  35. return _primaryhandobject;
  36. }
  37. }
  38. /// <summary>
  39. /// Objects that should always be parented to the primary hand, and will be moved when the hands are switched.
  40. /// </summary>
  41. [Space(5)]
  42. [Tooltip("Objects that should always be parented to the primary hand, and will be moved when the hands are switched.")]
  43. public List<GameObject> parentToPrimaryHand = new List<GameObject>();
  44. /// <summary>
  45. /// Objects that should always be parented to the secondary hand, and will be moved when the hands are switched.
  46. /// </summary>
  47. [Tooltip("Objects that should always be parented to the secondary hand, and will be moved when the hands are switched.")]
  48. public List<GameObject> parentToSecondaryHand = new List<GameObject>();
  49. /// <summary>
  50. /// Objects to enable only when the right hand is primary, and disabled otherwise.
  51. /// </summary>
  52. [Space(5)]
  53. [Tooltip("Objects to enable only when the right hand is primary, and disabled otherwise. ")]
  54. public List<GameObject> enableWhenRightIsPrimary = new List<GameObject>();
  55. /// <summary>
  56. /// Objects to enable only when the left hand is primary, and disabled otherwise.
  57. /// </summary>
  58. [Tooltip("Objects to enable only when the left hand is primary, and disabled otherwise. ")]
  59. public List<GameObject> enableWhenLeftIsPrimary = new List<GameObject>();
  60. /// <summary>
  61. /// Switches the handedness, calling events and shifting transforms around as necessary.
  62. /// </summary>
  63. /// <param name="righthanded"></param>
  64. public void SetPrimaryHand(bool righthanded)
  65. {
  66. print("Switching primary hand to " + (righthanded ? "right" : "left"));
  67. //First, move all the "parent to ___ hand" objecs to the correct one.
  68. Transform primaryhand = righthanded ? rightHand : leftHand;
  69. Transform secondaryhand = righthanded ? leftHand : rightHand;
  70. foreach (GameObject pobj in parentToPrimaryHand)
  71. {
  72. Vector3 localpos = pobj.transform.localPosition;
  73. Quaternion localrot = pobj.transform.localRotation;
  74. pobj.transform.SetParent(primaryhand, false);
  75. }
  76. foreach (GameObject sobj in parentToSecondaryHand)
  77. {
  78. Vector3 localpos = sobj.transform.localPosition;
  79. Quaternion localrot = sobj.transform.localRotation;
  80. sobj.transform.SetParent(secondaryhand, false);
  81. }
  82. //Now enable the objects for the correct hand.
  83. foreach(GameObject robj in enableWhenRightIsPrimary)
  84. {
  85. robj.SetActive(righthanded);
  86. }
  87. foreach(GameObject lobj in enableWhenLeftIsPrimary)
  88. {
  89. lobj.SetActive(!righthanded);
  90. }
  91. _primaryhandobject = primaryhand.gameObject;
  92. }
  93. }