MouseMiddleControlCamera.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 鼠标滚轮控制相机视野的变化
  6. /// </summary>
  7. public class MouseMiddleControlCamera : MonoBehaviour {
  8. [Header("相机视角的最小值")]
  9. [Range(0,100)]
  10. public float m_CameraFieldOfViewMin;
  11. [Header("相机视角的最大值")]
  12. [Range(0, 100)]
  13. public float m_CameraFieldOfViewMax;
  14. private void Awake()
  15. {
  16. m_CameraFieldOfViewMin = 15;
  17. m_CameraFieldOfViewMax = 100;
  18. }
  19. // Use this for initialization
  20. void Start () {
  21. }
  22. // Update is called once per frame
  23. void Update () {
  24. //鼠标滚轮的效果
  25. //camera.main.fieldofview摄像机的视野
  26. //Camera.main.orthographicSize摄像机的正交投影
  27. //Zoom out 缩小
  28. //GetAxis获得轴,Mouse ScrollWheel鼠标滚轮
  29. if (Input.GetAxis("Mouse ScrollWheel") < 0)
  30. {
  31. if (Camera.main.fieldOfView <= m_CameraFieldOfViewMax)
  32. Camera.main.fieldOfView += 2;
  33. if (Camera.main.orthographicSize <= 10)
  34. Camera.main.orthographicSize += 1f;
  35. }
  36. //Zoom in 放大
  37. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  38. {
  39. if (Camera.main.fieldOfView > m_CameraFieldOfViewMin)
  40. Camera.main.fieldOfView -= 2;
  41. if (Camera.main.orthographicSize >= 1)
  42. Camera.main.orthographicSize -= 1f;
  43. }
  44. }
  45. }