FitScreenAspect.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace EZXR.Glass.Core
  6. {
  7. public class FitScreenAspect : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// 缩放的基础宽度
  11. /// </summary>
  12. public int baseWidth = 640;
  13. /// <summary>
  14. /// 缩放的基础高度
  15. /// </summary>
  16. public int baseHeight = 480;
  17. public Camera myCamera;
  18. public RectTransform image;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. if (myCamera != null)
  23. {
  24. float width = baseWidth * ((float)Screen.height / baseHeight) / Screen.width;
  25. myCamera.rect = new Rect((1 - width) / 2.0f, 0, width, 1);
  26. }
  27. if (image != null)
  28. {
  29. float width = baseWidth * ((float)Screen.height / baseHeight);
  30. image.sizeDelta = new Vector2(width, Screen.height);
  31. }
  32. }
  33. // Update is called once per frame
  34. void Update()
  35. {
  36. }
  37. }
  38. }