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.

84 lines
3.0 KiB

  1. namespace VRTK.Examples.Utilities
  2. {
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class SceneChanger : MonoBehaviour
  6. {
  7. private bool canPress;
  8. private VRTK_ControllerReference controllerReference;
  9. private void Awake()
  10. {
  11. canPress = false;
  12. Invoke("ResetPress", 1f);
  13. DynamicGI.UpdateEnvironment();
  14. }
  15. private bool IsForwardPressed()
  16. {
  17. if (!VRTK_ControllerReference.IsValid(controllerReference))
  18. {
  19. return false;
  20. }
  21. if (canPress &&
  22. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Trigger, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  23. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Grip, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  24. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Touchpad, SDK_BaseController.ButtonPressTypes.Press, controllerReference))
  25. {
  26. return true;
  27. }
  28. return false;
  29. }
  30. private bool IsBackPressed()
  31. {
  32. if (!VRTK_ControllerReference.IsValid(controllerReference))
  33. {
  34. return false;
  35. }
  36. if (canPress &&
  37. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Trigger, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  38. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.Grip, SDK_BaseController.ButtonPressTypes.Press, controllerReference) &&
  39. VRTK_SDK_Bridge.GetControllerButtonState(SDK_BaseController.ButtonTypes.ButtonTwo, SDK_BaseController.ButtonPressTypes.Press, controllerReference))
  40. {
  41. return true;
  42. }
  43. return false;
  44. }
  45. private void ResetPress()
  46. {
  47. canPress = true;
  48. }
  49. private void Update()
  50. {
  51. GameObject rightHand = VRTK_DeviceFinder.GetControllerRightHand(true);
  52. controllerReference = VRTK_ControllerReference.GetControllerReference(rightHand);
  53. int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
  54. int nextSceneIndex = currentSceneIndex;
  55. if (IsForwardPressed() || Input.GetKeyUp(KeyCode.Space))
  56. {
  57. nextSceneIndex++;
  58. if (nextSceneIndex >= SceneManager.sceneCountInBuildSettings)
  59. nextSceneIndex = 0;
  60. }
  61. else if (IsBackPressed() || Input.GetKeyUp(KeyCode.Backspace))
  62. {
  63. nextSceneIndex--;
  64. if (nextSceneIndex < 0)
  65. nextSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
  66. }
  67. if (nextSceneIndex == currentSceneIndex)
  68. {
  69. return;
  70. }
  71. SceneManager.LoadScene(nextSceneIndex);
  72. }
  73. }
  74. }