-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathCameraSwitcher.cs
50 lines (42 loc) · 1.34 KB
/
CameraSwitcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Camera switcher, https://forum.unity3d.com/threads/how-can-i-switch-between-multiple-cameras-using-one-key-click.472009/
// usage: Assign cameras into the array, press C to switch into next camera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityLibrary
{
public class CameraSwitcher : MonoBehaviour
{
public Camera[] cameras;
int currentCamera = 0;
void Awake()
{
if (cameras == null || cameras.Length == 0)
{
Debug.LogError("No cameras assigned..", gameObject);
this.enabled = false;
}
EnableOnlyFirstCamera();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
// disable current
cameras[currentCamera].enabled = false;
// increment index and wrap after finished array
currentCamera = (currentCamera + 1) % cameras.Length;
// enable next
cameras[currentCamera].enabled = true;
}
}
void EnableOnlyFirstCamera()
{
for (int i = 0; i < cameras.Length; i++)
{
// only returns true when i is 0
cameras[i].enabled = (i == 0);
}
}
}
}