CameraSwitcher.cs 4.1 KB

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