SwitchCamera
From Unify Community Wiki
(Difference between revisions)
m (Text replace - "</javascript>" to "</syntaxhighlight>") |
m (Cleaned up code comments) |
||
Line 43: | Line 43: | ||
public Camera camera2; | public Camera camera2; | ||
− | |||
void Start () { | void Start () { | ||
camera1.enabled = true; | camera1.enabled = true; | ||
Line 49: | Line 48: | ||
} | } | ||
− | |||
void Update () { | void Update () { | ||
if (Input.GetKeyDown("2")) | if (Input.GetKeyDown("2")) | ||
Line 80: | Line 78: | ||
private int cameraIndex = 0; | private int cameraIndex = 0; | ||
− | |||
void Start () { | void Start () { | ||
cameraIndex = 0; | cameraIndex = 0; | ||
Line 134: | Line 131: | ||
private int cameraIndex = 0; | private int cameraIndex = 0; | ||
− | |||
function Start () { | function Start () { | ||
cameraIndex = 0; | cameraIndex = 0; |
Latest revision as of 20:59, 24 December 2013
Author: JakeH
Contents |
[edit] Description
This script lets you switch between two cameras in your scene.
[edit] Usage
Attach the script to amy GameObject and define the variables.
[edit] JavaScript- SwitchCamera.js
var camera1 : Camera; var camera2 : Camera; function Start () { camera1.enabled = true; camera2.enabled = false; } function Update () { if (Input.GetKeyDown ("2")){ camera1.enabled = false; camera2.enabled = true; } if (Input.GetKeyDown ("1")){ camera1.enabled = true; camera2.enabled = false; } }
[edit] CSharp- SwitchCamera.cs
using UnityEngine; using System.Collections; public class SwitchCamera : MonoBehaviour { public Camera camera1; public Camera camera2; void Start () { camera1.enabled = true; camera2.enabled = false; } void Update () { if (Input.GetKeyDown("2")) { camera1.enabled = false; camera2.enabled = true; } if (Input.GetKeyDown("1")) { camera1.enabled = true; camera2.enabled = false; } } }
[edit] More cameras
The scripts below use arrays to store as many cameras as wanted. The script must be placed on a camera for the controls to be displayed.
[edit] CSharp - SwitchCameras.cs
/** Author: Douglas Barcelos **/ using UnityEngine; using System.Collections; public class SwitchCameras : MonoBehaviour { public Camera[] cameras; private int cameraIndex = 0; void Start () { cameraIndex = 0; SelectCamera(cameraIndex); } void OnGUI() { if(GUI.Button(new Rect(Screen.width - 120, 20, 100, 100), "Next >>")) { if(cameraIndex >= cameras.Length - 1) cameraIndex = 0; else cameraIndex++; SelectCamera(cameraIndex); } if(GUI.Button(new Rect(20, 20, 100, 100), "<< Prev")) { if(cameraIndex <= 0) cameraIndex = cameras.Length - 1; else cameraIndex--; SelectCamera(cameraIndex); } } void SelectCamera(int idCamera) { if (cameras[idCamera] != null) { foreach(Camera cam in cameras) cam.enabled = false; cameras[idCamera].camera.enabled = true; } } }
[edit] Javascript - SwitchCameras.js
//////////////\\\\\\\\\\\\\\\ // Author: Douglas Barcelos // Translator: William Stott /////////////\\\\\\\\\\\\\\\ var cameras : Camera[]; private int cameraIndex = 0; function Start () { cameraIndex = 0; SelectCamera(cameraIndex); } function OnGUI() { if(GUI.Button(new Rect(Screen.width - 120, 20, 100, 100), "Next >>")) { if (cameraIndex >= cameras.Length - 1) { cameraIndex = 0; } else { cameraIndex++; } SelectCamera(cameraIndex); } if(GUI.Button(new Rect(20, 20, 100, 100), "<< Prev")) { if (cameraIndex <= 0) { cameraIndex = cameras.Length - 1; } else { cameraIndex--; } SelectCamera(cameraIndex); } } function SelectCamera (idCamera : int) { if (cameras[idCamera] != null) { for (cam : Camera in cameras) cam.enabled = false; cameras[idCamera].camera.enabled = true; } }