Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
8.3 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. using UnityEditor;
  5. using GrabAttachMechanics;
  6. using SecondaryControllerGrabActions;
  7. public class VRTK_ObjectSetup : EditorWindow
  8. {
  9. private enum PrimaryGrab
  10. {
  11. ChildOfController,
  12. FixedJoint,
  13. Climbable,
  14. CustomJoint,
  15. RotatorTrack,
  16. SpringJoint,
  17. TrackObject
  18. }
  19. private enum SecondaryGrab
  20. {
  21. SwapControllers,
  22. ControlDirection,
  23. AxisScale
  24. }
  25. private bool useGrab = true;
  26. private bool holdGrab = false;
  27. private bool useUse = false;
  28. private bool useIfGrabbed = false;
  29. private bool holdUse = false;
  30. private PrimaryGrab primGrab;
  31. private SecondaryGrab secGrab;
  32. private bool disableIdle = true;
  33. private bool addrb = true;
  34. private bool addHaptics = true;
  35. private Color touchColor = Color.clear;
  36. [MenuItem("Window/VRTK/Setup Interactable Object")]
  37. private static void Init()
  38. {
  39. VRTK_ObjectSetup window = (VRTK_ObjectSetup)EditorWindow.GetWindow(typeof(VRTK_ObjectSetup));
  40. window.minSize = new Vector2(300f, 370f);
  41. window.maxSize = new Vector2(400f, 400f);
  42. window.autoRepaintOnSceneChange = true;
  43. window.titleContent.text = "Setup Object";
  44. window.Show();
  45. }
  46. private void OnGUI()
  47. {
  48. EditorGUILayout.Space();
  49. EditorGUILayout.LabelField("Touch Options", EditorStyles.boldLabel);
  50. touchColor = EditorGUILayout.ColorField("Touch Highlight Color", touchColor);
  51. EditorGUILayout.Space();
  52. EditorGUILayout.LabelField("Grab Options", EditorStyles.boldLabel);
  53. useGrab = EditorGUILayout.Toggle("Is Grabbable", useGrab);
  54. holdGrab = EditorGUILayout.Toggle("Hold Button To Grab", holdGrab);
  55. EditorGUILayout.Space();
  56. primGrab = (PrimaryGrab)EditorGUILayout.EnumPopup("Grab Attach Mechanic", primGrab);
  57. secGrab = (SecondaryGrab)EditorGUILayout.EnumPopup("Secondary Grab Attach", secGrab);
  58. EditorGUILayout.Space();
  59. EditorGUILayout.LabelField("Use Options", EditorStyles.boldLabel);
  60. useUse = EditorGUILayout.Toggle("Is Usable", useUse);
  61. holdUse = EditorGUILayout.Toggle("Hold Button To Use", holdUse);
  62. useIfGrabbed = EditorGUILayout.Toggle("Use Only If Grabbed", useIfGrabbed);
  63. EditorGUILayout.Space();
  64. EditorGUILayout.LabelField("Misc Options", EditorStyles.boldLabel);
  65. disableIdle = EditorGUILayout.Toggle("Disable On Idle", disableIdle);
  66. addrb = EditorGUILayout.Toggle("Add RigidBody", addrb);
  67. addHaptics = EditorGUILayout.Toggle("Add Haptics", addHaptics);
  68. EditorGUILayout.Space();
  69. if (GUILayout.Button("Setup selected object(s)", GUILayout.Height(40)))
  70. {
  71. SetupObject();
  72. }
  73. }
  74. private void SetupObject()
  75. {
  76. Transform[] transforms = Selection.transforms;
  77. foreach (Transform currentTransform in transforms)
  78. {
  79. VRTK_InteractableObject interactableObject = SetupInteractableObject(currentTransform);
  80. SetupPrimaryGrab(interactableObject);
  81. SetupSecondaryGrab(interactableObject);
  82. SetupRigidbody(interactableObject);
  83. SetupHaptics(interactableObject);
  84. SetupHighlighter(interactableObject);
  85. }
  86. }
  87. private VRTK_InteractableObject SetupInteractableObject(Transform givenTransform)
  88. {
  89. VRTK_InteractableObject intObj = givenTransform.GetComponent<VRTK_InteractableObject>();
  90. if (intObj == null)
  91. {
  92. intObj = givenTransform.gameObject.AddComponent<VRTK_InteractableObject>();
  93. }
  94. intObj.isGrabbable = useGrab;
  95. intObj.holdButtonToGrab = holdGrab;
  96. intObj.isUsable = useUse;
  97. intObj.disableWhenIdle = disableIdle;
  98. intObj.grabOverrideButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  99. intObj.useOverrideButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  100. return intObj;
  101. }
  102. private void SetupPrimaryGrab(VRTK_InteractableObject givenObject)
  103. {
  104. VRTK_BaseGrabAttach grab = givenObject.GetComponentInChildren<VRTK_BaseGrabAttach>();
  105. if (grab != null)
  106. {
  107. DestroyImmediate(grab);
  108. }
  109. switch (primGrab)
  110. {
  111. case PrimaryGrab.ChildOfController:
  112. grab = givenObject.gameObject.AddComponent<VRTK_ChildOfControllerGrabAttach>();
  113. break;
  114. case PrimaryGrab.FixedJoint:
  115. grab = givenObject.gameObject.AddComponent<VRTK_FixedJointGrabAttach>();
  116. break;
  117. case PrimaryGrab.Climbable:
  118. grab = givenObject.gameObject.AddComponent<VRTK_ClimbableGrabAttach>();
  119. break;
  120. case PrimaryGrab.CustomJoint:
  121. grab = givenObject.gameObject.AddComponent<VRTK_CustomJointGrabAttach>();
  122. break;
  123. case PrimaryGrab.RotatorTrack:
  124. grab = givenObject.gameObject.AddComponent<VRTK_RotatorTrackGrabAttach>();
  125. break;
  126. case PrimaryGrab.SpringJoint:
  127. grab = givenObject.gameObject.AddComponent<VRTK_SpringJointGrabAttach>();
  128. break;
  129. case PrimaryGrab.TrackObject:
  130. grab = givenObject.gameObject.AddComponent<VRTK_TrackObjectGrabAttach>();
  131. break;
  132. default:
  133. grab = givenObject.gameObject.AddComponent<VRTK_ChildOfControllerGrabAttach>();
  134. break;
  135. }
  136. givenObject.grabAttachMechanicScript = grab;
  137. }
  138. private void SetupSecondaryGrab(VRTK_InteractableObject givenObject)
  139. {
  140. VRTK_BaseGrabAction grab = givenObject.GetComponentInChildren<VRTK_BaseGrabAction>();
  141. if (grab != null)
  142. {
  143. DestroyImmediate(grab);
  144. }
  145. switch (secGrab)
  146. {
  147. case SecondaryGrab.SwapControllers:
  148. grab = givenObject.gameObject.AddComponent<VRTK_SwapControllerGrabAction>();
  149. break;
  150. case SecondaryGrab.ControlDirection:
  151. grab = givenObject.gameObject.AddComponent<VRTK_ControlDirectionGrabAction>();
  152. break;
  153. case SecondaryGrab.AxisScale:
  154. grab = givenObject.gameObject.AddComponent<VRTK_AxisScaleGrabAction>();
  155. break;
  156. default:
  157. grab = givenObject.gameObject.AddComponent<VRTK_SwapControllerGrabAction>();
  158. break;
  159. }
  160. givenObject.secondaryGrabActionScript = grab;
  161. }
  162. private void SetupRigidbody(VRTK_InteractableObject givenObject)
  163. {
  164. if (addrb)
  165. {
  166. Rigidbody rb = givenObject.GetComponent<Rigidbody>();
  167. if (rb == null)
  168. {
  169. givenObject.gameObject.AddComponent<Rigidbody>();
  170. }
  171. }
  172. }
  173. private void SetupHaptics(VRTK_InteractableObject givenObject)
  174. {
  175. if (addHaptics)
  176. {
  177. VRTK_InteractHaptics haptics = givenObject.GetComponentInChildren<VRTK_InteractHaptics>();
  178. if (haptics == null)
  179. {
  180. givenObject.gameObject.AddComponent<VRTK_InteractHaptics>();
  181. }
  182. }
  183. }
  184. private void SetupHighlighter(VRTK_InteractableObject givenObject)
  185. {
  186. if (touchColor != Color.clear)
  187. {
  188. VRTK_InteractObjectHighlighter highlighter = givenObject.GetComponentInChildren<VRTK_InteractObjectHighlighter>();
  189. if (highlighter == null)
  190. {
  191. highlighter = givenObject.gameObject.AddComponent<VRTK_InteractObjectHighlighter>();
  192. }
  193. highlighter.touchHighlight = touchColor;
  194. }
  195. }
  196. }
  197. }