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.

108 lines
3.7 KiB

  1. namespace VRTK.Examples.Utilities
  2. {
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class VRTKExample_SceneSwitcher : MonoBehaviour
  6. {
  7. public KeyCode backKey = KeyCode.Backspace;
  8. public KeyCode forwardKey = KeyCode.Space;
  9. protected int firstSceneIndex = 0;
  10. protected int lastSceneIndex;
  11. protected bool pressEnabled;
  12. protected VRTK_ControllerReference controllerReference;
  13. protected virtual void Awake()
  14. {
  15. DynamicGI.UpdateEnvironment();
  16. }
  17. protected virtual void OnEnable()
  18. {
  19. lastSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
  20. pressEnabled = false;
  21. Invoke("EnablePress", 1f);
  22. }
  23. protected virtual void Update()
  24. {
  25. GameObject rightHand = VRTK_DeviceFinder.GetControllerRightHand(true);
  26. controllerReference = VRTK_ControllerReference.GetControllerReference(rightHand);
  27. int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
  28. int nextSceneIndex = currentSceneIndex;
  29. if (ForwardPressed())
  30. {
  31. nextSceneIndex++;
  32. if (nextSceneIndex >= lastSceneIndex)
  33. {
  34. nextSceneIndex = firstSceneIndex;
  35. }
  36. }
  37. else if (BackPressed())
  38. {
  39. nextSceneIndex--;
  40. if (nextSceneIndex < firstSceneIndex)
  41. {
  42. nextSceneIndex = lastSceneIndex - 1;
  43. }
  44. }
  45. if (nextSceneIndex != currentSceneIndex)
  46. {
  47. SceneManager.LoadScene(nextSceneIndex);
  48. }
  49. }
  50. protected virtual void EnablePress()
  51. {
  52. pressEnabled = true;
  53. }
  54. protected virtual bool BackPressed()
  55. {
  56. if (Input.GetKeyDown(backKey) || ControllerBackward())
  57. {
  58. return true;
  59. }
  60. return false;
  61. }
  62. protected virtual bool ForwardPressed()
  63. {
  64. if (Input.GetKeyDown(forwardKey) || ControllerForward())
  65. {
  66. return true;
  67. }
  68. return false;
  69. }
  70. protected virtual bool ControllerForward()
  71. {
  72. if (!VRTK_ControllerReference.IsValid(controllerReference))
  73. {
  74. return false;
  75. }
  76. return (pressEnabled &&
  77. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.ButtonTwo, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  78. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Touchpad, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  79. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Trigger, SDK_BaseController.ButtonPressTypes.Press, controllerReference));
  80. }
  81. protected virtual bool ControllerBackward()
  82. {
  83. if (!VRTK_ControllerReference.IsValid(controllerReference))
  84. {
  85. return false;
  86. }
  87. return (pressEnabled &&
  88. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.ButtonTwo, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  89. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Touchpad, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  90. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Grip, SDK_BaseController.ButtonPressTypes.Press, controllerReference));
  91. }
  92. }
  93. }