假设我们在pc上使用的是1024x768, 那么我们如何适应android不同手机的分辨率的?
我这里的处理是,首先Editor.bat创建一个UI,并且保存成game_ui.xml
那么下来具体Lua代码如下
// 这里是加载UI的
local game = ui:LoadLayout(cache:GetResource("XMLFile", "UI/game_ui.xml"));
game:SetPosition(0, 0);
ui.root:AddChild(game);
// 下面是针对的一些处理
local w = graphics.width;
local h = graphics.height;
local sw = self.uWidth / w
local sh = self.uHeight / h
// 我们计算出一个合理的缩放比率
local ss = math.min(sw, sh);
local uw = w * ss;
local uh = h * ss;
// 重新设置UI的比率缩放
ui:SetWidth(uw)
ui:SetHeight(uh);
// 重新设置我们的UI比率
self.mWingame:SetFixedSize(uw, uh);
self.mWingame:UpdateLayout();
基本上就是这样了,可能如果需要的话需要对
SubscribeToEvent("ScreenMode","HandleScreenMode")
// 这个屏幕变换事件进行一下处理看具体情况