CameraChange.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Sirenix.OdinInspector;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class CameraChange : MonoBehaviour
  6. {
  7. private static CameraChange _instance;
  8. public static CameraChange instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. {
  14. _instance = FindObjectOfType<CameraChange>();
  15. }
  16. return _instance;
  17. }
  18. }
  19. [SerializeField]
  20. public List<CameraInfo> m_CameraInfo;
  21. private Camera m_CurrentCamera;
  22. void Start()
  23. {
  24. foreach (var cameraInfo in m_CameraInfo)
  25. {
  26. cameraInfo.SwitchedCamera.GetComponent<CameraSurround_Principle>().SetCameraPosition(cameraInfo.InitCamera.transform, true, false);
  27. }
  28. m_CurrentCamera = m_CameraInfo[0].InitCamera;
  29. }
  30. public void SwitchCamera(int index)
  31. {
  32. m_CurrentCamera = m_CameraInfo[index].InitCamera;
  33. }
  34. public void CameraFree(bool isPlay)
  35. {
  36. foreach (var camInfo in m_CameraInfo)
  37. {
  38. if (m_CurrentCamera == camInfo.InitCamera&& isPlay)
  39. {
  40. // 当前是自由相机 → 切回动画相机
  41. camInfo.SwitchedCamera.gameObject.SetActive(false);
  42. camInfo.InitCamera.enabled = true;
  43. //Debug.LogError("切回动画相机" + camInfo.SwitchedCamera.gameObject.name);
  44. //Debug.LogError("切回动画相机" + camInfo.InitCamera.gameObject.name);
  45. return;
  46. }
  47. else if (m_CurrentCamera == camInfo.InitCamera && !isPlay)
  48. {
  49. // 当前是动画相机 → 切到自由相机
  50. camInfo.InitCamera.enabled = false;
  51. camInfo.SwitchedCamera.gameObject.SetActive(true);
  52. camInfo.SwitchedCamera.GetComponent<CameraSurround_Principle>().SetCameraPosition(camInfo.InitCamera.transform, true, false);
  53. //Debug.LogError("切到自由相机" + camInfo.SwitchedCamera.gameObject.name);
  54. //Debug.LogError("切到自由相机" + camInfo.InitCamera.gameObject.name);
  55. return;
  56. }
  57. Debug.LogWarning("当前相机不在相机对列表中!");
  58. }
  59. }
  60. [System.Serializable]
  61. public class CameraInfo
  62. {
  63. [LabelText("动画相机")]
  64. public Camera InitCamera;
  65. [LabelText("暂停动画相机")]
  66. public Camera SwitchedCamera;
  67. }
  68. }