MultipleCameraSwitcher2

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Adding script to switch between arbitrary numbers of cameras)
 
(added audio listener switching)
Line 10: Line 10:
 
==Usage==
 
==Usage==
 
Drag the script onto a GameObject and view it in the inspector. Size the ''Cameras'' array to how many cameras you need, then drag the cameras on it's entries. Finally, set ''Camera Index'' to the index (zero-based) of the camera slot to start with.
 
Drag the script onto a GameObject and view it in the inspector. Size the ''Cameras'' array to how many cameras you need, then drag the cameras on it's entries. Finally, set ''Camera Index'' to the index (zero-based) of the camera slot to start with.
 +
 +
If you don't want the script to change the audio listener to the current camera, uncheck ''Switch Audio Listener''.
  
 
To switch between camera views, the default button is '''C'''.  
 
To switch between camera views, the default button is '''C'''.  
Line 19: Line 21:
 
var cameras : Camera[];
 
var cameras : Camera[];
 
var cameraIndex : int = 0;
 
var cameraIndex : int = 0;
 +
var switchAudioListener = true;
  
 
function Start ()  
 
function Start ()  
 
{
 
{
    if (cameras.length < 1) {
+
if (cameras.length < 1) {
        Debug.Log("No cameras set.");
+
Debug.LogError("No cameras set.");
        return;
+
return;
    }
+
}
  
    for (var c : Camera in cameras) {
+
for (var c : Camera in cameras) {
        c.enabled = false;
+
ToggleCam(c, false);
    }
+
}
  
    if ((cameraIndex < 0) || (cameraIndex >= cameras.length)) {
+
if ((cameraIndex < 0) || (cameraIndex >= cameras.length)) {
        Debug.Log("Invalid camera index.");
+
Debug.LogError("Invalid camera index.");
        cameraIndex = 0;
+
cameraIndex = 0;
    }
+
}
 
+
ToggleCam(cameras[cameraIndex], true);
    cameras[cameraIndex].enabled = true;
+
 
}  
 
}  
 +
 +
function ToggleCam(cam : Camera, enabled : boolean)
 +
{
 +
cam.enabled = enabled;
 +
if (switchAudioListener) {
 +
var listener = cam.GetComponent(AudioListener);
 +
if (listener) {
 +
listener.enabled = enabled;
 +
}
 +
}
 +
}
  
 
function Update ()  
 
function Update ()  
 
{  
 
{  
    if (Input.GetKeyDown ("c"))
+
if (Input.GetKeyDown ("c"))
    {  
+
{  
        cameras[cameraIndex].enabled = false;
+
ToggleCam(cameras[cameraIndex], false);
        cameraIndex = (cameraIndex + 1) % cameras.length;
+
cameraIndex = (cameraIndex + 1) % cameras.length;
        cameras[cameraIndex].enabled = true;
+
ToggleCam(cameras[cameraIndex], true);
    }
+
}
 
}
 
}
 
</javascript>
 
</javascript>

Revision as of 21:20, 25 October 2011


Author: cgf

Description

A script that allows you to switch between different cameras in your scene. This does what MultipleCameraSwitcher does without hard-coding the number of cameras.

Usage

Drag the script onto a GameObject and view it in the inspector. Size the Cameras array to how many cameras you need, then drag the cameras on it's entries. Finally, set Camera Index to the index (zero-based) of the camera slot to start with.

If you don't want the script to change the audio listener to the current camera, uncheck Switch Audio Listener.

To switch between camera views, the default button is C.

JavaScript - MultipleCameraSwitcher2.js

<javascript>

  1. pragma strict

var cameras : Camera[]; var cameraIndex : int = 0; var switchAudioListener = true;

function Start () { if (cameras.length < 1) { Debug.LogError("No cameras set."); return; }

for (var c : Camera in cameras) { ToggleCam(c, false); }

if ((cameraIndex < 0) || (cameraIndex >= cameras.length)) { Debug.LogError("Invalid camera index."); cameraIndex = 0; } ToggleCam(cameras[cameraIndex], true); }

function ToggleCam(cam : Camera, enabled : boolean) { cam.enabled = enabled; if (switchAudioListener) { var listener = cam.GetComponent(AudioListener); if (listener) { listener.enabled = enabled; } } }

function Update () { if (Input.GetKeyDown ("c")) { ToggleCam(cameras[cameraIndex], false); cameraIndex = (cameraIndex + 1) % cameras.length; ToggleCam(cameras[cameraIndex], true); } } </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox