--适配工具 AdapterUtility = {} local this = AdapterUtility local lastSafeArea = CS.UnityEngine.Rect(0, 0, 0, 0) local lastLandscapeState = "" local Screen = CS.UnityEngine.Screen ---编辑器下测试刘海屏 local isEditorTest = false --特殊机型可能需要特殊处理 local cfg_mobile_special = { { brand = "COOLPAD", model = "SEA-A0", notchHeight = 50 }, } local function CheckSpecial(brand, model) local upBrand = string.upper(brand) for k, v in pairs(cfg_mobile_special) do if string.contains(upBrand, v.brand) and v.model == model then return true, v.notchHeight end end return false, 0 end function this.Refresh() if not UIManager.adapter_WindowCanvasUI then return end local orientation = Screen.orientation:ToString() if lastLandscapeState == orientation then return end local safeArea = MobileInfo.Instance.SafeArea local srcScreenWidth = LoginManager.srcScreenWidth local srcScreenHeight = LoginManager.srcScreenHeight if LoginInfo.IsSDK() then local brand = MuInterface.Instance:getDeviceBrand() local model = MuInterface.Instance:getSystemModel() local ret, notchHeight = CheckSpecial(brand, model) if ret then safeArea = CS.UnityEngine.Rect(notchHeight, 0, srcScreenWidth - 2 * notchHeight, srcScreenHeight) end end if isEditorTest then safeArea = CS.UnityEngine.Rect(100, 0, srcScreenWidth - 200, srcScreenHeight) end lastLandscapeState = orientation if safeArea ~= lastSafeArea then lastSafeArea = safeArea local anchorMin = safeArea.position local anchorMax = safeArea.position + safeArea.size anchorMin.x = anchorMin.x / srcScreenWidth anchorMin.y = 0 anchorMax.x = anchorMax.x / srcScreenWidth anchorMax.y = 1 logNoFlag("anchorMin=("..anchorMin.x ..","..anchorMin.y..")" ,"anchorMax=("..anchorMax.x ..","..anchorMax.y..")") UIManager.adapter_WindowCanvasUI.anchorMin = anchorMin UIManager.adapter_WindowCanvasUI.anchorMax = anchorMax end end ---@return void @走cfg_phoneModel适配了,后续看情况是否需求启动,如果cfg_phoneModel配置里面没找到,调用这个 function this.SetResolution() if Main.isWindows and (LoginInfo.IsBox() or LoginInfo.IsBoxSDK()) then return end local srcScreenWidth = LoginManager.srcScreenWidth local srcScreenHeight = LoginManager.srcScreenHeight local screenRatio = srcScreenWidth / srcScreenHeight; local defaultHeight = 750 local defaultWidth = 1334 local defaultRoata = defaultWidth / defaultHeight --以高度适配 if screenRatio >= defaultRoata then local newHeight = Mathf.Min(Screen.height,defaultHeight) local newWidth = Mathf.Ceil(newHeight*screenRatio) logNoFlag('以高度适配:newWidth='..newWidth..' newHeight='..newHeight.." 原始分辨率="..srcScreenWidth..'*'..srcScreenHeight) LoginManager.setScreenWidth = newWidth LoginManager.setScreenHeight = newHeight Screen.SetResolution(newWidth,newHeight,not Main.isWindows) else local newWidth =Mathf.Min(Screen.width,defaultWidth) local newHeight = Mathf.Ceil(newWidth/screenRatio) LoginManager.setScreenWidth = newWidth LoginManager.setScreenHeight = newHeight logNoFlag('以宽度适配:newWidth='..newWidth..' newHeight='..newHeight.." 原始分辨率="..srcScreenWidth..'*'..srcScreenHeight) Screen.SetResolution(newWidth,newHeight,not Main.isWindows) end end --根据机型在初始化的时候设置,策划觉得走低中高设置比较好 function this.SetResolutionV1() local cpuName = nil local mobileCfg = nil local isPhone = false --初始化MobileInfo 调用Init local safeArea = MobileInfo.Instance.SafeArea if CS.TCFramework.Platform.isIOS then isPhone = true cpuName = "iPhone" CS.UnityEngine.Debug.Log("IOS cpuName=iPhone"); elseif CS.TCFramework.Platform.isAndroid then isPhone = true cpuName = CS.MobileInfo.Instance.CpuName or "" CS.UnityEngine.Debug.Log("Android cpuName="..cpuName); end --if isPhone and CS.UnityEngine.Application.isMobilePlatform then if isPhone and CS.UnityEngine.Application.isMobilePlatform then if qualityType == EGameQualityType.PowerSave then mobileCfg = CS.UnityEngine.Application.platform == CS.UnityEngine.RuntimePlatform.IPhonePlayer and cfg_appleModelGetItem(cpuName) or cfg_phoneModelGetItem(cpuName) if string.isNullOrEmpty(cpuName) then --如果拿不到CPU信息,java-readCpuInfo里面拿到信息里面不包含hardware,设置成省电模式 AdapterUtility.SetResolution() DataManager.settingData:SetDefaultQualityType(EGameQualityType.PowerSave) CS.UnityEngine.Debug.Log("InitResolution cpuName is empty"); elseif mobileCfg == nil then --如果拿到了CPU信息,但是不在配表内,打印一个报错,让其传到后台日志,手动添加 logError("InitResolution mobileCfg is nil 手动添加一下=" .. cpuName); --UISettingMgr.SetResolution(1) AdapterUtility.SetResolution() DataManager.settingData:SetDefaultQualityType(EGameQualityType.PowerSave) else CS.UnityEngine.Debug.Log("InitResolution mobileCfg.resolution=" .. mobileCfg.resolution); UISettingMgr.SetResolution(mobileCfg.resolution) DataManager.settingData:SetDefaultQualityType(mobileCfg.quality + 1) end elseif qualityType == EGameQualityType.Balance then AdapterUtility.SetResolution() else CS.UnityEngine.Screen.SetResolution(Main.wInitScreenSize, Main.hInitScreenSize, true); end end end --根据低中高设置分辨率 local lastGameQualityType = nil local qualityResolution = { [EGameQualityType.Strongest] = 1, [EGameQualityType.Balance] = 0.85, [EGameQualityType.PowerSave] = 0.7, } function this.SetResolutionV2(gameQualityType) if not gameQualityType then return end if lastGameQualityType == gameQualityType then return end lastGameQualityType = gameQualityType local scale = qualityResolution[gameQualityType] or 1 UISettingMgr.SetResolution(scale) end