Unity 框架

FixScreenRatio.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /****************************************************
  2. 文件:FixScreenRatio.cs
  3. 作者:陶长春
  4. 邮箱:376248129@qq.com
  5. 日期:2025年3月13日 9:10:37
  6. UnityVersion: 2021.3.13f1
  7. 功能:UI适配
  8. *****************************************************/
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using TFramework;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. namespace TFramework
  16. {
  17. public class FixScreenRatio : MonoBehaviour
  18. {
  19. public enum Orientation
  20. {
  21. Portrait,
  22. Landscape
  23. }
  24. public float defaultModifier = 0.5f;
  25. public Vector2 currentRes = new Vector2();
  26. public Vector2 newRes = new Vector2();
  27. public float refWidth = 480;
  28. public float refHeight = 320;
  29. public Orientation refOrientation = Orientation.Portrait;
  30. void OnEnable()
  31. {
  32. // Initially update scale
  33. newRes.x = Screen.width;
  34. newRes.y = Screen.height;
  35. UpdateScaling();
  36. }
  37. void Update()
  38. {
  39. // Update if resolution has changed
  40. newRes.x = Screen.width;
  41. newRes.y = Screen.height;
  42. if (newRes != currentRes)
  43. {
  44. UpdateScaling();
  45. }
  46. }
  47. private void OnValidate()
  48. {
  49. UpdateScaling();
  50. }
  51. /// <summary>
  52. /// Handles updating of the canvas scale based on device resolution
  53. /// </summary>
  54. public virtual void UpdateScaling()
  55. {
  56. currentRes = newRes;
  57. // Determine platform specific resolution multiplier
  58. float multiplier = 1f;
  59. #if UNITY_ANDROID || UNITY_IOS
  60. switch (refOrientation)
  61. {
  62. case Orientation.Portrait:
  63. multiplier = 1f + ((((float)currentRes.y - refHeight) / refHeight) * defaultModifier);
  64. break;
  65. case Orientation.Landscape:
  66. multiplier = 1f + ((((float)currentRes.x - refWidth) / refWidth) * defaultModifier);
  67. break;
  68. }
  69. #else
  70. // For desktop platforms we scale based on the largest value out of width & height
  71. if (currentRes.x > currentRes.y)
  72. {
  73. multiplier = 1f + ((((float)currentRes.y - refHeight) / refHeight) * defaultModifier);
  74. }
  75. else
  76. {
  77. multiplier = 1f + ((((float)currentRes.x - refWidth) / refWidth) * defaultModifier);
  78. }
  79. #endif
  80. // Modify multiplier based on initial value.
  81. if (multiplier < 0.5f) // Ceil to 0.5 if less than 0.5
  82. {
  83. multiplier = 0.5f;
  84. }
  85. else if (multiplier < 2f) // Calc to the nearest 0.5 if less than 2
  86. {
  87. multiplier = Mathf.Max(1f, (float)(Math.Round(((double)multiplier * 2), MidpointRounding.AwayFromZero) / 2));
  88. }
  89. else // Otherwise if larger than 2 then round
  90. {
  91. multiplier = (float)Math.Round(multiplier, MidpointRounding.AwayFromZero);
  92. }
  93. // Apply new scale factor to canvas
  94. GetComponent<CanvasScaler>().scaleFactor = multiplier;
  95. }
  96. }
  97. }