3 namespace UnityEngine
.Experimental
.Rendering
5 public class CameraSwitcher
: MonoBehaviour
7 public Camera
[] m_Cameras
;
9 private int m_CurrentCameraIndex
= -1;
10 private Camera m_OriginalCamera
= null;
11 private Vector3 m_OriginalCameraPosition
;
12 private Quaternion m_OriginalCameraRotation
;
13 private Camera m_CurrentCamera
= null;
15 GUIContent
[] m_CameraNames
= null;
16 int[] m_CameraIndices
= null;
18 DebugUI
.EnumField m_DebugEntry
;
20 //saved enum fields for when repainting
21 int m_DebugEntryEnumIndex
;
25 m_OriginalCamera
= GetComponent
<Camera
>();
26 m_CurrentCamera
= m_OriginalCamera
;
28 if (m_OriginalCamera
== null)
30 Debug
.LogError("Camera Switcher needs a Camera component attached");
34 m_CurrentCameraIndex
= GetCameraCount() - 1;
36 m_CameraNames
= new GUIContent
[GetCameraCount()];
37 m_CameraIndices
= new int[GetCameraCount()];
39 for (int i
= 0; i
< m_Cameras
.Length
; ++i
)
41 Camera cam
= m_Cameras
[i
];
44 m_CameraNames
[i
] = new GUIContent(cam
.name
);
48 m_CameraNames
[i
] = new GUIContent("null");
50 m_CameraIndices
[i
] = i
;
53 m_CameraNames
[GetCameraCount() - 1] = new GUIContent("Original Camera");
54 m_CameraIndices
[GetCameraCount() - 1] = GetCameraCount() - 1;
56 m_DebugEntry
= new DebugUI
.EnumField { displayName = "Camera Switcher", getter = () => m_CurrentCameraIndex, setter = value => SetCameraIndex(value), enumNames = m_CameraNames, enumValues = m_CameraIndices, getIndex = () => m_DebugEntryEnumIndex, setIndex = value => m_DebugEntryEnumIndex = value }
;
57 var panel
= DebugManager
.instance
.GetPanel("Camera", true);
58 panel
.children
.Add(m_DebugEntry
);
63 if (m_DebugEntry
!= null && m_DebugEntry
.panel
!= null)
65 var panel
= m_DebugEntry
.panel
;
66 panel
.children
.Remove(m_DebugEntry
);
72 return m_Cameras
.Length
+ 1; // We need +1 for handling the original camera.
75 Camera
GetNextCamera()
77 if (m_CurrentCameraIndex
== m_Cameras
.Length
)
78 return m_OriginalCamera
;
80 return m_Cameras
[m_CurrentCameraIndex
];
83 void SetCameraIndex(int index
)
85 if (index
> 0 || index
< GetCameraCount())
87 m_CurrentCameraIndex
= index
;
89 if (m_CurrentCamera
== m_OriginalCamera
)
91 m_OriginalCameraPosition
= m_OriginalCamera
.transform
.position
;
92 m_OriginalCameraRotation
= m_OriginalCamera
.transform
.rotation
;
95 m_CurrentCamera
= GetNextCamera();
97 if (m_CurrentCamera
!= null)
99 // If we witch back to the original camera, put back the transform in it.
100 if (m_CurrentCamera
== m_OriginalCamera
)
102 m_OriginalCamera
.transform
.position
= m_OriginalCameraPosition
;
103 m_OriginalCamera
.transform
.rotation
= m_OriginalCameraRotation
;
106 transform
.position
= m_CurrentCamera
.transform
.position
;
107 transform
.rotation
= m_CurrentCamera
.transform
.rotation
;