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.

131 lines
5.3 KiB

  1. #if UNITY_EDITOR
  2. namespace VRTK.Examples.Utilities
  3. {
  4. using UnityEngine;
  5. using UnityEditor;
  6. [ExecuteInEditMode]
  7. public class VRTKExample_FixSetup : MonoBehaviour
  8. {
  9. public bool forceOculusFloorLevel = true;
  10. protected bool trackingLevelFloor = false;
  11. public virtual void ApplyFixes()
  12. {
  13. FixOculus();
  14. }
  15. protected virtual void Awake()
  16. {
  17. if (Application.isEditor && !Application.isPlaying)
  18. {
  19. ApplyFixes();
  20. }
  21. }
  22. protected virtual void Update()
  23. {
  24. FixTrackingType();
  25. }
  26. protected virtual void FixTrackingType()
  27. {
  28. #if VRTK_DEFINE_SDK_OCULUS
  29. if (forceOculusFloorLevel && !trackingLevelFloor)
  30. {
  31. GameObject overManagerGO = GameObject.Find("[VRTK_SDKManager]/[VRTK_SDKSetups]/Oculus/OVRCameraRig");
  32. if (overManagerGO != null)
  33. {
  34. OVRManager ovrManager = overManagerGO.GetComponent<OVRManager>();
  35. if (ovrManager != null)
  36. {
  37. ovrManager.trackingOriginType = OVRManager.TrackingOrigin.FloorLevel;
  38. trackingLevelFloor = true;
  39. Debug.Log("Forced Oculus Tracking to Floor Level");
  40. }
  41. }
  42. }
  43. #endif
  44. }
  45. protected virtual void FixOculus()
  46. {
  47. #if VRTK_DEFINE_SDK_OCULUS
  48. string oculusPath = "[VRTK_SDKManager]/[VRTK_SDKSetups]/Oculus";
  49. GameObject oculusSDK = GameObject.Find(oculusPath);
  50. /*
  51. if (oculusSDK == null || oculusSDK.GetComponentInChildren<OVRManager>() != null)
  52. {
  53. Debug.Log("No Oculus Repaired Required");
  54. return;
  55. }
  56. */
  57. GameObject currentRig = GameObject.Find(oculusPath + "/OVRCameraRig");
  58. GameObject currentAvatar = GameObject.Find(oculusPath + "/LocalAvatar");
  59. VRTK_SDKSetup oculusSetup = oculusSDK.GetComponent<VRTK_SDKSetup>();
  60. if (currentRig != null)
  61. {
  62. DestroyImmediate(currentRig);
  63. }
  64. if (currentAvatar != null)
  65. {
  66. DestroyImmediate(currentAvatar);
  67. }
  68. GameObject ovrCameraRig = null;
  69. GameObject ovrCameraRigToInstantiate = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Oculus/VR/Prefabs/OVRCameraRig.prefab", typeof(GameObject));
  70. if (ovrCameraRigToInstantiate != null)
  71. {
  72. ovrCameraRig = Instantiate(ovrCameraRigToInstantiate) as GameObject;
  73. }
  74. if (ovrCameraRig != null)
  75. {
  76. ovrCameraRig.name = ovrCameraRig.name.Replace("(Clone)", "");
  77. ovrCameraRig.transform.SetParent(oculusSDK.transform);
  78. ovrCameraRig.SetActive(true);
  79. oculusSetup.actualBoundaries = ovrCameraRig;
  80. oculusSetup.actualHeadset = GameObject.Find(oculusPath + "/OVRCameraRig/TrackingSpace/CenterEyeAnchor");
  81. oculusSetup.actualLeftController = GameObject.Find(oculusPath + "/OVRCameraRig/TrackingSpace/LeftHandAnchor");
  82. oculusSetup.actualRightController = GameObject.Find(oculusPath + "/OVRCameraRig/TrackingSpace/RightHandAnchor");
  83. Debug.Log("Successfully repaired Oculus OVRCameraRig prefab");
  84. }
  85. GameObject ovrAvatar = null;
  86. GameObject ovrAvatarToInstantiate = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Oculus/Avatar/Content/Prefabs/LocalAvatar.prefab", typeof(GameObject));
  87. if (ovrAvatarToInstantiate != null)
  88. {
  89. ovrAvatar = Instantiate(ovrAvatarToInstantiate) as GameObject;
  90. }
  91. if (ovrAvatar == null)
  92. {
  93. //legacy location
  94. ovrAvatarToInstantiate = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/OvrAvatar/Content/Prefabs/LocalAvatar.prefab", typeof(GameObject));
  95. if (ovrAvatarToInstantiate != null)
  96. {
  97. ovrAvatar = Instantiate(ovrAvatarToInstantiate) as GameObject;
  98. }
  99. }
  100. if (ovrAvatar != null)
  101. {
  102. OvrAvatar avatarScript = ovrAvatar.GetComponent<OvrAvatar>();
  103. avatarScript.StartWithControllers = true;
  104. ovrAvatar.name = ovrAvatar.name.Replace("(Clone)", "");
  105. ovrAvatar.transform.SetParent(oculusSDK.transform);
  106. ovrAvatar.SetActive(true);
  107. oculusSetup.modelAliasLeftController = GameObject.Find(oculusPath + "/LocalAvatar/controller_left");
  108. oculusSetup.modelAliasRightController = GameObject.Find(oculusPath + "/LocalAvatar/controller_right");
  109. //GameObject.Find(oculusPath + "/LocalAvatar/hand_left").SetActive(false);
  110. //GameObject.Find(oculusPath + "/LocalAvatar/hand_right").SetActive(false);
  111. VRTK_TransformFollow transformFollow = ovrAvatar.AddComponent<VRTK_TransformFollow>();
  112. transformFollow.gameObjectToFollow = ovrCameraRig;
  113. Debug.Log("Successfully repaired Oculus LocalAvatar prefab");
  114. }
  115. #endif
  116. }
  117. }
  118. }
  119. #endif