SwitchCamera

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Simplified by using more specific types)
Line 57: Line 57:
 
}
 
}
 
}
 
}
 +
}
 +
</csharp>
 +
 +
==Multiple Cameras==
 +
This script supports multiple cameras simultaneously.
 +
 +
<csharp>
 +
 +
/** Author: Douglas Barcelos **/
 +
 +
 +
using UnityEngine;
 +
using System.Collections;
 +
 +
public class SwitchCameras : MonoBehaviour {
 +
 +
public Camera[] cameras;
 +
private int cameraIndex = 0;
 +
 +
// Use this for initialization
 +
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;
 +
}
 +
}
 
}
 
}
 
</csharp>
 
</csharp>

Revision as of 12:43, 18 October 2010

Author: JakeH

Contents

Description

This script lets you switch between two cameras in your scene.

Usage

Attach the script to amy GameObject and define the variables.

JavaScript- SwitchCamera.js

<javascript> 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; 
  }     

} </javascript>

CSharp- SwitchCamera.cs

<csharp> using UnityEngine; using System.Collections;

public class SwitchCamera : MonoBehaviour {

public Camera camera1; public Camera camera2;

// Use this for initialization void Start () { camera1.enabled = true; camera2.enabled = false; }

// Update is called once per frame void Update () { if (Input.GetKeyDown("2")) { camera1.enabled = false; camera2.enabled = true; } if (Input.GetKeyDown("1")) { camera1.enabled = true; camera2.enabled = false; } } } </csharp>

Multiple Cameras

This script supports multiple cameras simultaneously.

<csharp>

/** Author: Douglas Barcelos **/


using UnityEngine; using System.Collections;

public class SwitchCameras : MonoBehaviour {

public Camera[] cameras; private int cameraIndex = 0;

// Use this for initialization 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; } } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox