|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using UnityEngine; |
4 | 4 |
|
5 | | -public class CameraSwitcher : MonoBehaviour |
| 5 | +namespace UnityEngine.Experimental.Rendering |
6 | 6 | { |
7 | | - public Camera[] m_Cameras; |
8 | | - |
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; |
| 7 | + public class CameraSwitcher : MonoBehaviour |
| 8 | + { |
| 9 | + public Camera[] m_Cameras; |
14 | 10 |
|
15 | | - private float m_MessageDuration = 1.0f; |
16 | | - private float m_MessageTimer = 1000.0f; |
| 11 | + private int m_CurrentCameraIndex = -1; |
| 12 | + private Camera m_OriginalCamera = null; |
| 13 | + private Vector3 m_OriginalCameraPosition; |
| 14 | + private Quaternion m_OriginalCameraRotation; |
| 15 | + private Camera m_CurrentCamera = null; |
17 | 16 |
|
18 | | - private static string kDebugNext = "Debug Next"; |
19 | | - private static string kDebugPrevious = "Debug Previous"; |
20 | | - private string[] m_RequiredInputButtons = { kDebugNext, kDebugPrevious }; |
21 | | - private bool m_Valid = true; |
| 17 | + private float m_MessageDuration = 1.0f; |
| 18 | + private float m_MessageTimer = 1000.0f; |
22 | 19 |
|
23 | | - void OnEnable() |
24 | | - { |
25 | | - m_OriginalCamera = GetComponent<Camera>(); |
26 | | - m_CurrentCamera = m_OriginalCamera; |
27 | | - |
28 | | - m_Valid = Debugging.CheckRequiredInputButtonMapping(m_RequiredInputButtons); |
29 | | - } |
| 20 | + private static string kDebugNext = "Debug Next"; |
| 21 | + private static string kDebugPrevious = "Debug Previous"; |
| 22 | + private string[] m_RequiredInputButtons = { kDebugNext, kDebugPrevious }; |
| 23 | + private bool m_Valid = true; |
30 | 24 |
|
31 | | - int GetCameraCount() |
32 | | - { |
33 | | - return m_Cameras.Length + 1; // We need +1 for handling the original camera. |
34 | | - } |
| 25 | + void OnEnable() |
| 26 | + { |
| 27 | + m_OriginalCamera = GetComponent<Camera>(); |
| 28 | + m_CurrentCamera = m_OriginalCamera; |
35 | 29 |
|
36 | | - void NextCamera() |
37 | | - { |
38 | | - m_CurrentCameraIndex = (m_CurrentCameraIndex + 1) % GetCameraCount(); |
39 | | - } |
| 30 | + m_Valid = Debugging.CheckRequiredInputButtonMapping(m_RequiredInputButtons); |
| 31 | + } |
40 | 32 |
|
41 | | - void PreviousCamera() |
42 | | - { |
43 | | - m_CurrentCameraIndex = m_CurrentCameraIndex - 1; |
44 | | - if (m_CurrentCameraIndex == -1) |
45 | | - m_CurrentCameraIndex = m_Cameras.Length; |
46 | | - } |
| 33 | + int GetCameraCount() |
| 34 | + { |
| 35 | + return m_Cameras.Length + 1; // We need +1 for handling the original camera. |
| 36 | + } |
47 | 37 |
|
48 | | - Camera GetNextCamera() |
49 | | - { |
50 | | - if (m_CurrentCameraIndex == m_Cameras.Length) |
51 | | - return m_OriginalCamera; |
52 | | - else |
53 | | - return m_Cameras[m_CurrentCameraIndex]; |
54 | | - } |
| 38 | + void NextCamera() |
| 39 | + { |
| 40 | + m_CurrentCameraIndex = (m_CurrentCameraIndex + 1) % GetCameraCount(); |
| 41 | + } |
55 | 42 |
|
56 | | - void Update() |
57 | | - { |
58 | | - if (m_Valid && Debugging.debugControlEnabled && m_OriginalCamera != null) |
| 43 | + void PreviousCamera() |
59 | 44 | { |
60 | | - m_MessageTimer += Time.deltaTime; |
61 | | - bool needUpdateCamera = false; |
62 | | - if (Input.GetButtonDown(kDebugNext)) |
63 | | - { |
64 | | - NextCamera(); |
65 | | - needUpdateCamera = true; |
66 | | - } |
| 45 | + m_CurrentCameraIndex = m_CurrentCameraIndex - 1; |
| 46 | + if (m_CurrentCameraIndex == -1) |
| 47 | + m_CurrentCameraIndex = m_Cameras.Length; |
| 48 | + } |
67 | 49 |
|
68 | | - if (Input.GetButtonDown(kDebugPrevious)) |
69 | | - { |
70 | | - PreviousCamera(); |
71 | | - needUpdateCamera = true; |
72 | | - } |
| 50 | + Camera GetNextCamera() |
| 51 | + { |
| 52 | + if (m_CurrentCameraIndex == m_Cameras.Length) |
| 53 | + return m_OriginalCamera; |
| 54 | + else |
| 55 | + return m_Cameras[m_CurrentCameraIndex]; |
| 56 | + } |
73 | 57 |
|
74 | | - if (needUpdateCamera) |
| 58 | + void Update() |
| 59 | + { |
| 60 | + if (m_Valid && Debugging.debugControlEnabled && m_OriginalCamera != null) |
75 | 61 | { |
76 | | - m_MessageTimer = 0.0f; |
77 | | - |
78 | | - if(m_CurrentCamera == m_OriginalCamera) |
| 62 | + m_MessageTimer += Time.deltaTime; |
| 63 | + bool needUpdateCamera = false; |
| 64 | + if (Input.GetButtonDown(kDebugNext)) |
79 | 65 | { |
80 | | - m_OriginalCameraPosition = m_OriginalCamera.transform.position; |
81 | | - m_OriginalCameraRotation = m_OriginalCamera.transform.rotation; |
| 66 | + NextCamera(); |
| 67 | + needUpdateCamera = true; |
82 | 68 | } |
83 | 69 |
|
84 | | - m_CurrentCamera = GetNextCamera(); |
| 70 | + if (Input.GetButtonDown(kDebugPrevious)) |
| 71 | + { |
| 72 | + PreviousCamera(); |
| 73 | + needUpdateCamera = true; |
| 74 | + } |
85 | 75 |
|
86 | | - if(m_CurrentCamera != null) |
| 76 | + if (needUpdateCamera) |
87 | 77 | { |
88 | | - // If we witch back to the original camera, put back the transform in it. |
89 | | - if (m_CurrentCamera == m_OriginalCamera) |
| 78 | + m_MessageTimer = 0.0f; |
| 79 | + |
| 80 | + if(m_CurrentCamera == m_OriginalCamera) |
90 | 81 | { |
91 | | - m_OriginalCamera.transform.position = m_OriginalCameraPosition; |
92 | | - m_OriginalCamera.transform.rotation = m_OriginalCameraRotation; |
| 82 | + m_OriginalCameraPosition = m_OriginalCamera.transform.position; |
| 83 | + m_OriginalCameraRotation = m_OriginalCamera.transform.rotation; |
93 | 84 | } |
94 | 85 |
|
95 | | - transform.position = m_CurrentCamera.transform.position; |
96 | | - transform.rotation = m_CurrentCamera.transform.rotation; |
| 86 | + m_CurrentCamera = GetNextCamera(); |
| 87 | + |
| 88 | + if(m_CurrentCamera != null) |
| 89 | + { |
| 90 | + // If we witch back to the original camera, put back the transform in it. |
| 91 | + if (m_CurrentCamera == m_OriginalCamera) |
| 92 | + { |
| 93 | + m_OriginalCamera.transform.position = m_OriginalCameraPosition; |
| 94 | + m_OriginalCamera.transform.rotation = m_OriginalCameraRotation; |
| 95 | + } |
| 96 | + |
| 97 | + transform.position = m_CurrentCamera.transform.position; |
| 98 | + transform.rotation = m_CurrentCamera.transform.rotation; |
| 99 | + } |
97 | 100 | } |
98 | | - } |
99 | 101 |
|
100 | | - if (m_MessageTimer < m_MessageDuration) |
101 | | - { |
102 | | - string cameraName = m_CurrentCamera != null ? m_CurrentCamera.name : "NULL"; |
103 | | - string message = string.Format("Switching to camera : {0}", cameraName); |
104 | | - Debugging.PushDebugMessage(message); |
| 102 | + if (m_MessageTimer < m_MessageDuration) |
| 103 | + { |
| 104 | + string cameraName = m_CurrentCamera != null ? m_CurrentCamera.name : "NULL"; |
| 105 | + string message = string.Format("Switching to camera : {0}", cameraName); |
| 106 | + Debugging.PushDebugMessage(message); |
| 107 | + } |
105 | 108 | } |
106 | 109 | } |
107 | 110 | } |
|
0 commit comments