CameraAdapter.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. namespace Utility.UIAdapter
  3. {
  4. public class CameraAdapter : MonoBehaviour
  5. {
  6. private float _width;
  7. private float _height;
  8. /// <summary>
  9. /// 宽高比
  10. /// </summary>
  11. private float _ratio;
  12. private float _defaultFOV = 50;
  13. private Vector2 _sizeDelta;
  14. /// <summary>
  15. /// 设计宽高比
  16. /// </summary>
  17. private float _designRatio;
  18. private Camera _camera;
  19. void Start()
  20. {
  21. _width = Screen.width;
  22. _height = Screen.height;
  23. _ratio = _width / _height;
  24. _designRatio = 1334f / 750f;
  25. _camera = gameObject.GetComponent<Camera>();
  26. if (_camera.orthographic)
  27. {
  28. _camera.orthographicSize = _designRatio / _ratio * _camera.orthographicSize;
  29. }
  30. else
  31. {
  32. if (_designRatio / _ratio * _defaultFOV < 50)
  33. {
  34. _camera.fieldOfView = 50;
  35. }
  36. else
  37. {
  38. _camera.fieldOfView = _designRatio / _ratio * _defaultFOV;
  39. }
  40. }
  41. }
  42. }
  43. }