CameraChange.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. m_CurrentCamera = m_CameraInfo[0].InitCamera;
  25. }
  26. public void SwitchCamera(int index)
  27. {
  28. m_CurrentCamera = m_CameraInfo[index].InitCamera;
  29. }
  30. public void CameraFree(bool isPlay)
  31. {
  32. foreach (var camInfo in m_CameraInfo)
  33. {
  34. if (m_CurrentCamera == camInfo.InitCamera&& isPlay)
  35. {
  36. // 当前是自由相机 → 切回动画相机
  37. camInfo.SwitchedCamera.gameObject.SetActive(false);
  38. camInfo.InitCamera.enabled = true;
  39. //Debug.LogError("切回动画相机" + camInfo.SwitchedCamera.gameObject.name);
  40. //Debug.LogError("切回动画相机" + camInfo.InitCamera.gameObject.name);
  41. return;
  42. }
  43. else if (m_CurrentCamera == camInfo.InitCamera && !isPlay)
  44. {
  45. camInfo.SwitchedCamera.GetComponent<FreeCameraController>().SnapTo(camInfo.InitCamera.transform);
  46. // 当前是动画相机 → 切到自由相机
  47. camInfo.InitCamera.enabled = false;
  48. camInfo.SwitchedCamera.gameObject.SetActive(true);
  49. //Debug.LogError("切到自由相机" + camInfo.SwitchedCamera.gameObject.name);
  50. //Debug.LogError("切到自由相机" + camInfo.InitCamera.gameObject.name);
  51. return;
  52. }
  53. Debug.LogWarning("当前相机不在相机对列表中!");
  54. }
  55. }
  56. [System.Serializable]
  57. public class CameraInfo
  58. {
  59. [LabelText("动画相机")]
  60. public Camera InitCamera;
  61. [LabelText("暂停动画相机")]
  62. public Camera SwitchedCamera;
  63. }
  64. }