A game about forced loneliness, made by TACStudios
1using UnityEngine;
2
3namespace UnityEngine.Rendering
4{
5 /// <summary>
6 /// Utility component allowing users to setup many different static camera and cycle through their positions using the Debug Window.
7 /// </summary>
8 [CoreRPHelpURLAttribute("Camera-Switcher")]
9 public class CameraSwitcher : MonoBehaviour
10 {
11 /// <summary>
12 /// List of target cameras.
13 /// </summary>
14 public Camera[] m_Cameras;
15
16 private int m_CurrentCameraIndex = -1;
17 private Camera m_OriginalCamera = null;
18 private Vector3 m_OriginalCameraPosition;
19 private Quaternion m_OriginalCameraRotation;
20 private Camera m_CurrentCamera = null;
21
22 GUIContent[] m_CameraNames = null;
23 int[] m_CameraIndices = null;
24
25 DebugUI.EnumField m_DebugEntry;
26
27 //saved enum fields for when repainting
28 int m_DebugEntryEnumIndex;
29
30 void OnEnable()
31 {
32 m_OriginalCamera = GetComponent<Camera>();
33 m_CurrentCamera = m_OriginalCamera;
34
35 if (m_OriginalCamera == null)
36 {
37 Debug.LogError("Camera Switcher needs a Camera component attached");
38 return;
39 }
40
41 m_CurrentCameraIndex = GetCameraCount() - 1;
42
43 m_CameraNames = new GUIContent[GetCameraCount()];
44 m_CameraIndices = new int[GetCameraCount()];
45
46 for (int i = 0; i < m_Cameras.Length; ++i)
47 {
48 Camera cam = m_Cameras[i];
49 if (cam != null)
50 {
51 m_CameraNames[i] = new GUIContent(cam.name);
52 }
53 else
54 {
55 m_CameraNames[i] = new GUIContent("null");
56 }
57 m_CameraIndices[i] = i;
58 }
59
60 m_CameraNames[GetCameraCount() - 1] = new GUIContent("Original Camera");
61 m_CameraIndices[GetCameraCount() - 1] = GetCameraCount() - 1;
62
63 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 };
64 var panel = DebugManager.instance.GetPanel("Camera", true);
65 panel.children.Add(m_DebugEntry);
66 }
67
68 void OnDisable()
69 {
70 if (m_DebugEntry != null && m_DebugEntry.panel != null)
71 {
72 var panel = m_DebugEntry.panel;
73 panel.children.Remove(m_DebugEntry);
74 }
75 }
76
77 int GetCameraCount()
78 {
79 return m_Cameras.Length + 1; // We need +1 for handling the original camera.
80 }
81
82 Camera GetNextCamera()
83 {
84 if (m_CurrentCameraIndex == m_Cameras.Length)
85 return m_OriginalCamera;
86 else
87 return m_Cameras[m_CurrentCameraIndex];
88 }
89
90 void SetCameraIndex(int index)
91 {
92 if (index > 0 && index < GetCameraCount())
93 {
94 m_CurrentCameraIndex = index;
95
96 if (m_CurrentCamera == m_OriginalCamera)
97 {
98 m_OriginalCameraPosition = m_OriginalCamera.transform.position;
99 m_OriginalCameraRotation = m_OriginalCamera.transform.rotation;
100 }
101
102 m_CurrentCamera = GetNextCamera();
103
104 if (m_CurrentCamera != null)
105 {
106 // If we witch back to the original camera, put back the transform in it.
107 if (m_CurrentCamera == m_OriginalCamera)
108 {
109 m_OriginalCamera.transform.position = m_OriginalCameraPosition;
110 m_OriginalCamera.transform.rotation = m_OriginalCameraRotation;
111 }
112
113 transform.position = m_CurrentCamera.transform.position;
114 transform.rotation = m_CurrentCamera.transform.rotation;
115 }
116 }
117 }
118 }
119}