-- 显示物品需求
if GUI:Win_IsNotNull(ui.itemroom) and next_data then
GUI:removeAllChildren(ui.itemroom)
local item_name = next_data.name2 or "称号卷轴"
local itemCount = tonumber(SL:GetMetaValue("ITEM_COUNT", item_name)) o
要实现手机端和PC端位置一致,需要根据屏幕分辨率进行适配。996引擎提供了获取屏幕尺寸的接口,我们可以根据当前屏幕尺寸计算相对位置。
```lua
-- 显示物品需求
if GUI:Win_IsNotNull(ui.itemroom) and next_data then
GUI:removeAllChildren(ui.itemroom)
local item_name = next_data.name2 or "称号卷轴"
local itemCount = tonumber(SL:GetMetaValue("ITEM_COUNT", item_name)) or 0
local needItemCount = tonumber(next_data.value2) or 0
-- 金币检查
local goldCount = tonumber(SL:GetMetaValue("MONEY", 1)) or 0
local needGold = tonumber(next_data.value1) or 0
-- 获取物品ID
local item_id = 0
if next_data.itemid then
item_id = tonumber(next_data.itemid) or 12453
else
-- 自定义函数:需要你实现lib:getitemidx()函数
item_id = lib:getitemidx(item_name) or 12453
end
-- 获取货币ID
local money_id = tonumber(next_data.moneyid) or 1
-- 安全比较函数
local function safe_lt(a, b)
local num_a = tonumber(a) or 0
local num_b = tonumber(b) or 0
return num_a < num_b
end
-- 根据是否足够设置颜色
local itemColor = 255
if safe_lt(itemCount, needItemCount) then
itemColor = 58
end
local goldColor = 255
if safe_lt(goldCount, needGold) then
goldColor = 58
end
-- 获取屏幕尺寸
local screenWidth = tonumber(SL:GetMetaValue("SCREEN_WIDTH")) or 1280
local screenHeight = tonumber(SL:GetMetaValue("SCREEN_HEIGHT")) or 720
-- 计算相对位置(基于1280x720基准分辨率)
local baseWidth = 1280
local baseHeight = 720
-- 计算缩放比例
local scaleX = screenWidth / baseWidth
local scaleY = screenHeight / baseHeight
local minScale = math.min(scaleX, scaleY) -- 使用最小缩放保持比例
-- 基准位置(在1280x720下的位置)
local baseItemX = 140
local baseItemY = 90
local baseGoldX = 53
local baseGoldY = 90
-- 计算适配后的位置
local itemX = baseItemX * minScale
local itemY = baseItemY * minScale
local goldX = baseGoldX * minScale
local goldY = baseGoldY * minScale
-- 创建称号卷轴显示
GUI:ItemShow_Create(ui.itemroom, "ItemShow_2", itemX, itemY, {
index = item_id,
count = needItemCount,
look = true,
bgVisible = false,
color = itemColor
})
-- 创建金币显示
GUI:ItemShow_Create(ui.itemroom, "ItemShow_1", goldX, goldY, {
index = money_id,
count = needGold,
look = true,
bgVisible = false,
color = goldColor
})
end
```
**关键点说明:**
1. **获取屏幕尺寸**:使用`SL:GetMetaValue("SCREEN_WIDTH")`和`SL:GetMetaValue("SCREEN_HEIGHT")`获取当前设备的屏幕分辨率
2. **基准分辨率**:我假设你的设计稿是基于1280x720分辨率,如果不是,请修改`baseWidth`和`baseHeight`为你的设计稿尺寸
3. **缩放计算**:
- 计算当前屏幕与基准屏幕的宽高比
- 使用`math.min(scaleX, scaleY)`确保UI元素不会超出屏幕边界
- 这样在不同设备上都能保持相对位置一致
4. **位置适配**:将基准位置乘以缩放比例得到适配后的位置
5. **注意**:`lib:getitemidx()`函数在知识库中不存在,你需要自己实现这个函数来获取物品ID
这样写就能确保在手机端和PC端显示位置保持一致了。如果还有问题,可以问浮生梦老师!