Last active
December 27, 2016 21:05
-
-
Save SnugglePilot/c272ef7cf4e3922abbdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class CompanionCamera : MonoBehaviour { | |
// Debug shortcut keys: | |
public KeyCode modifier; | |
public KeyCode toggleButton; | |
// If true, this will permanently disable the SteamVR companion camera. | |
// If false, it'll leave the SteamVR companion camera in the rotation. | |
public bool replaceSteamCamera; | |
List<GameObject> cameras; | |
SteamVR_GameView vrView; | |
int currentIndex = 0; | |
public bool isVRView { | |
get { | |
if (replaceSteamCamera) { | |
return currentIndex == 0 || currentIndex == 1; | |
} else { | |
return currentIndex == 0; | |
} | |
} | |
} | |
public Vector3 CurrentCameraPosition() { | |
return cameras[currentIndex].transform.position; | |
} | |
public Quaternion CurrentCameraRotation() { | |
// The camera might be on a child object and have a different rotation than it's parent object: | |
return cameras[currentIndex].GetComponentInChildren<Camera>().transform.rotation; | |
} | |
void Awake() { | |
cameras = new List<GameObject>(); | |
cameras.Add (null); // SteamVR camera placeholder | |
vrView = GameObject.FindObjectOfType<SteamVR_GameView>(); | |
for (int i = 0; i < transform.childCount; i++) { | |
cameras.Add((GameObject) transform.GetChild(i).gameObject); | |
} | |
if (replaceSteamCamera) { | |
// Immediately disable SteamVR camera. | |
SwitchCamera(); | |
} | |
} | |
public void SwitchCamera() { | |
if (cameras == null || cameras.Count <= 0) { | |
Debug.LogWarning ("No companion cameras registered"); | |
return; | |
} | |
if (currentIndex == 0) { | |
vrView.enabled = false; | |
} else { | |
cameras[currentIndex].SetActive (false); | |
} | |
currentIndex ++; | |
if (currentIndex >= cameras.Count) { | |
if (replaceSteamCamera) { | |
currentIndex = 1; | |
} else { | |
currentIndex = 0; | |
} | |
} | |
if (currentIndex == 0) { | |
vrView.enabled = Globals.settings.viveBuild; // so we don't enable it if it isn't running | |
} else { | |
cameras[currentIndex].SetActive (true); | |
} | |
} | |
void Update () { | |
if (modifier == KeyCode.None || Input.GetKey (modifier)) { | |
if (Input.GetKeyDown (toggleButton)) { | |
SwitchCamera (); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment