Skip to content

Commit 80db756

Browse files
committed
fix(Examples): rebuild oculus camera rig without a prefab
The PrefabUtility caused so many issues when attempting to create a linked prefab with the camera rig fix. Instead this just uses instantiate which doesn't have the same issues.
1 parent 2266c0a commit 80db756

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

Assets/VRTK/Examples/ExampleResources/SharedResources/Scripts/VRTKExample_FixSetup.cs

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,60 @@ namespace VRTK.Examples.Utilities
77
[ExecuteInEditMode]
88
public class VRTKExample_FixSetup : MonoBehaviour
99
{
10+
public bool forceOculusFloorLevel = true;
11+
protected bool trackingLevelFloor = false;
12+
1013
public virtual void ApplyFixes()
1114
{
1215
FixOculus();
1316
}
1417

1518
protected virtual void Awake()
1619
{
17-
ApplyFixes();
20+
21+
if (Application.isEditor && !Application.isPlaying)
22+
{
23+
ApplyFixes();
24+
}
25+
}
26+
27+
protected virtual void Update()
28+
{
29+
FixTrackingType();
30+
}
31+
32+
protected virtual void FixTrackingType()
33+
{
34+
#if VRTK_DEFINE_SDK_OCULUS
35+
if (forceOculusFloorLevel && !trackingLevelFloor)
36+
{
37+
GameObject overManagerGO = GameObject.Find("[VRTK_SDKManager]/[VRTK_SDKSetups]/Oculus/OVRCameraRig");
38+
if (overManagerGO != null)
39+
{
40+
OVRManager ovrManager = overManagerGO.GetComponent<OVRManager>();
41+
if (ovrManager != null)
42+
{
43+
ovrManager.trackingOriginType = OVRManager.TrackingOrigin.FloorLevel;
44+
trackingLevelFloor = true;
45+
Debug.Log("Forced Oculus Tracking to Floor Level");
46+
}
47+
}
48+
}
49+
#endif
1850
}
1951

2052
protected virtual void FixOculus()
2153
{
2254
#if VRTK_DEFINE_SDK_OCULUS
2355
string oculusPath = "[VRTK_SDKManager]/[VRTK_SDKSetups]/Oculus";
2456
GameObject oculusSDK = GameObject.Find(oculusPath);
57+
58+
if (oculusSDK == null || oculusSDK.GetComponentInChildren<OVRManager>() != null)
59+
{
60+
Debug.Log("No Oculus Repaired Required");
61+
return;
62+
}
63+
2564
GameObject currentRig = GameObject.Find(oculusPath + "/OVRCameraRig");
2665
GameObject currentAvatar = GameObject.Find(oculusPath + "/LocalAvatar");
2766
VRTK_SDKSetup oculusSetup = oculusSDK.GetComponent<VRTK_SDKSetup>();
@@ -35,30 +74,47 @@ protected virtual void FixOculus()
3574
DestroyImmediate(currentAvatar);
3675
}
3776

38-
GameObject ovrCameraRig = PrefabUtility.InstantiatePrefab((GameObject)AssetDatabase.LoadAssetAtPath("Assets/Oculus/VR/Prefabs/OVRCameraRig.prefab", typeof(GameObject))) as GameObject;
77+
GameObject ovrCameraRig = null;
78+
GameObject ovrCameraRigToInstantiate = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Oculus/VR/Prefabs/OVRCameraRig.prefab", typeof(GameObject));
79+
if (ovrCameraRigToInstantiate != null)
80+
{
81+
ovrCameraRig = Instantiate(ovrCameraRigToInstantiate) as GameObject;
82+
}
3983
if (ovrCameraRig != null)
4084
{
85+
ovrCameraRig.name = ovrCameraRig.name.Replace("(Clone)", "");
4186
ovrCameraRig.transform.SetParent(oculusSDK.transform);
42-
ovrCameraRig.SetActive(false);
87+
ovrCameraRig.SetActive(true);
4388
oculusSetup.actualBoundaries = ovrCameraRig;
4489
oculusSetup.actualHeadset = GameObject.Find(oculusPath + "/OVRCameraRig/TrackingSpace/CenterEyeAnchor");
4590
oculusSetup.actualLeftController = GameObject.Find(oculusPath + "/OVRCameraRig/TrackingSpace/LeftHandAnchor");
4691
oculusSetup.actualRightController = GameObject.Find(oculusPath + "/OVRCameraRig/TrackingSpace/RightHandAnchor");
47-
OVRManager ovrManager = ovrCameraRig.GetComponent<OVRManager>();
48-
ovrManager.trackingOriginType = OVRManager.TrackingOrigin.FloorLevel;
4992
Debug.Log("Successfully repaired Oculus OVRCameraRig prefab");
5093
}
5194

52-
GameObject ovrAvatar = PrefabUtility.InstantiatePrefab((GameObject)AssetDatabase.LoadAssetAtPath("Assets/Oculus/Avatar/Content/Prefabs/LocalAvatar.prefab", typeof(GameObject))) as GameObject;
95+
GameObject ovrAvatar = null;
96+
GameObject ovrAvatarToInstantiate = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Oculus/Avatar/Content/Prefabs/LocalAvatar.prefab", typeof(GameObject));
97+
if (ovrAvatarToInstantiate != null)
98+
{
99+
ovrAvatar = Instantiate(ovrAvatarToInstantiate) as GameObject;
100+
}
101+
53102
if (ovrAvatar == null)
54103
{
55104
//legacy location
56-
ovrAvatar = PrefabUtility.InstantiatePrefab((GameObject)AssetDatabase.LoadAssetAtPath("Assets/OvrAvatar/Content/Prefabs/LocalAvatar.prefab", typeof(GameObject))) as GameObject;
105+
ovrAvatarToInstantiate = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/OvrAvatar/Content/Prefabs/LocalAvatar.prefab", typeof(GameObject));
106+
if (ovrAvatarToInstantiate != null)
107+
{
108+
ovrAvatar = Instantiate(ovrAvatarToInstantiate) as GameObject;
109+
}
57110
}
58111
if (ovrAvatar != null)
59112
{
113+
OvrAvatar avatarScript = ovrAvatar.GetComponent<OvrAvatar>();
114+
avatarScript.StartWithControllers = true;
115+
ovrAvatar.name = ovrAvatar.name.Replace("(Clone)", "");
60116
ovrAvatar.transform.SetParent(oculusSDK.transform);
61-
ovrAvatar.SetActive(false);
117+
ovrAvatar.SetActive(true);
62118
oculusSetup.modelAliasLeftController = GameObject.Find(oculusPath + "/LocalAvatar/controller_left");
63119
oculusSetup.modelAliasRightController = GameObject.Find(oculusPath + "/LocalAvatar/controller_right");
64120
GameObject.Find(oculusPath + "/LocalAvatar/hand_left").SetActive(false);

0 commit comments

Comments
 (0)