AdapterUtility_Tips.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. --适配工具
  2. AdapterUtility = {}
  3. local this = AdapterUtility
  4. local lastSafeArea = CS.UnityEngine.Rect(0, 0, 0, 0)
  5. local lastLandscapeState = ""
  6. local Screen = CS.UnityEngine.Screen
  7. ---编辑器下测试刘海屏
  8. local isEditorTest = false
  9. --特殊机型可能需要特殊处理
  10. local cfg_mobile_special = {
  11. { brand = "COOLPAD", model = "SEA-A0", notchHeight = 50 },
  12. }
  13. local function CheckSpecial(brand, model)
  14. local upBrand = string.upper(brand)
  15. for k, v in pairs(cfg_mobile_special) do
  16. if string.contains(upBrand, v.brand) and v.model == model then
  17. return true, v.notchHeight
  18. end
  19. end
  20. return false, 0
  21. end
  22. function this.Refresh()
  23. if not UIManager.adapter_WindowCanvasUI then return end
  24. local orientation = Screen.orientation:ToString()
  25. if lastLandscapeState == orientation then return end
  26. local safeArea = MobileInfo.Instance.SafeArea
  27. local srcScreenWidth = LoginManager.srcScreenWidth
  28. local srcScreenHeight = LoginManager.srcScreenHeight
  29. if LoginInfo.IsSDK() then
  30. local brand = MuInterface.Instance:getDeviceBrand()
  31. local model = MuInterface.Instance:getSystemModel()
  32. local ret, notchHeight = CheckSpecial(brand, model)
  33. if ret then
  34. safeArea = CS.UnityEngine.Rect(notchHeight, 0, srcScreenWidth - 2 * notchHeight, srcScreenHeight)
  35. end
  36. end
  37. if isEditorTest then
  38. safeArea = CS.UnityEngine.Rect(100, 0, srcScreenWidth - 200, srcScreenHeight)
  39. end
  40. lastLandscapeState = orientation
  41. if safeArea ~= lastSafeArea then
  42. lastSafeArea = safeArea
  43. local anchorMin = safeArea.position
  44. local anchorMax = safeArea.position + safeArea.size
  45. anchorMin.x = anchorMin.x / srcScreenWidth
  46. anchorMin.y = 0
  47. anchorMax.x = anchorMax.x / srcScreenWidth
  48. anchorMax.y = 1
  49. logNoFlag("anchorMin=("..anchorMin.x ..","..anchorMin.y..")" ,"anchorMax=("..anchorMax.x ..","..anchorMax.y..")")
  50. UIManager.adapter_WindowCanvasUI.anchorMin = anchorMin
  51. UIManager.adapter_WindowCanvasUI.anchorMax = anchorMax
  52. end
  53. end
  54. ---@return void @走cfg_phoneModel适配了,后续看情况是否需求启动,如果cfg_phoneModel配置里面没找到,调用这个
  55. function this.SetResolution()
  56. if Main.isWindows and (LoginInfo.IsBox() or LoginInfo.IsBoxSDK()) then
  57. return
  58. end
  59. local srcScreenWidth = LoginManager.srcScreenWidth
  60. local srcScreenHeight = LoginManager.srcScreenHeight
  61. local screenRatio = srcScreenWidth / srcScreenHeight;
  62. local defaultHeight = 750
  63. local defaultWidth = 1334
  64. local defaultRoata = defaultWidth / defaultHeight
  65. --以高度适配
  66. if screenRatio >= defaultRoata then
  67. local newHeight = Mathf.Min(Screen.height,defaultHeight)
  68. local newWidth = Mathf.Ceil(newHeight*screenRatio)
  69. logNoFlag('以高度适配:newWidth='..newWidth..' newHeight='..newHeight.." 原始分辨率="..srcScreenWidth..'*'..srcScreenHeight)
  70. LoginManager.setScreenWidth = newWidth
  71. LoginManager.setScreenHeight = newHeight
  72. Screen.SetResolution(newWidth,newHeight,not Main.isWindows)
  73. else
  74. local newWidth =Mathf.Min(Screen.width,defaultWidth)
  75. local newHeight = Mathf.Ceil(newWidth/screenRatio)
  76. LoginManager.setScreenWidth = newWidth
  77. LoginManager.setScreenHeight = newHeight
  78. logNoFlag('以宽度适配:newWidth='..newWidth..' newHeight='..newHeight.." 原始分辨率="..srcScreenWidth..'*'..srcScreenHeight)
  79. Screen.SetResolution(newWidth,newHeight,not Main.isWindows)
  80. end
  81. end
  82. --根据机型在初始化的时候设置,策划觉得走低中高设置比较好
  83. function this.SetResolutionV1()
  84. local cpuName = nil
  85. local mobileCfg = nil
  86. local isPhone = false
  87. --初始化MobileInfo 调用Init
  88. local safeArea = MobileInfo.Instance.SafeArea
  89. if CS.TCFramework.Platform.isIOS then
  90. isPhone = true
  91. cpuName = "iPhone"
  92. CS.UnityEngine.Debug.Log("IOS cpuName=iPhone");
  93. elseif CS.TCFramework.Platform.isAndroid then
  94. isPhone = true
  95. cpuName = CS.MobileInfo.Instance.CpuName or ""
  96. CS.UnityEngine.Debug.Log("Android cpuName="..cpuName);
  97. end
  98. --if isPhone and CS.UnityEngine.Application.isMobilePlatform then
  99. if isPhone and CS.UnityEngine.Application.isMobilePlatform then
  100. if qualityType == EGameQualityType.PowerSave then
  101. mobileCfg = CS.UnityEngine.Application.platform == CS.UnityEngine.RuntimePlatform.IPhonePlayer and cfg_appleModelGetItem(cpuName) or cfg_phoneModelGetItem(cpuName)
  102. if string.isNullOrEmpty(cpuName) then
  103. --如果拿不到CPU信息,java-readCpuInfo里面拿到信息里面不包含hardware,设置成省电模式
  104. AdapterUtility.SetResolution()
  105. DataManager.settingData:SetDefaultQualityType(EGameQualityType.PowerSave)
  106. CS.UnityEngine.Debug.Log("InitResolution cpuName is empty");
  107. elseif mobileCfg == nil then
  108. --如果拿到了CPU信息,但是不在配表内,打印一个报错,让其传到后台日志,手动添加
  109. logError("InitResolution mobileCfg is nil 手动添加一下=" .. cpuName);
  110. --UISettingMgr.SetResolution(1)
  111. AdapterUtility.SetResolution()
  112. DataManager.settingData:SetDefaultQualityType(EGameQualityType.PowerSave)
  113. else
  114. CS.UnityEngine.Debug.Log("InitResolution mobileCfg.resolution=" .. mobileCfg.resolution);
  115. UISettingMgr.SetResolution(mobileCfg.resolution)
  116. DataManager.settingData:SetDefaultQualityType(mobileCfg.quality + 1)
  117. end
  118. elseif qualityType == EGameQualityType.Balance then
  119. AdapterUtility.SetResolution()
  120. else
  121. CS.UnityEngine.Screen.SetResolution(Main.wInitScreenSize, Main.hInitScreenSize, true);
  122. end
  123. end
  124. end
  125. --根据低中高设置分辨率
  126. local lastGameQualityType = nil
  127. local qualityResolution =
  128. {
  129. [EGameQualityType.Strongest] = 1,
  130. [EGameQualityType.Balance] = 0.85,
  131. [EGameQualityType.PowerSave] = 0.7,
  132. }
  133. function this.SetResolutionV2(gameQualityType)
  134. if not gameQualityType then
  135. return
  136. end
  137. if lastGameQualityType == gameQualityType then
  138. return
  139. end
  140. lastGameQualityType = gameQualityType
  141. local scale = qualityResolution[gameQualityType] or 1
  142. UISettingMgr.SetResolution(scale)
  143. end