Bag = {}
function Bag.Init(isWin32)
-- 网格配置
Bag._ScrollHeight = isWin32 and 214 or 320 -- 容器滚动区域的高度
Bag._PWidth = isWin32 and 338 or 500 -- 容器可见区域 宽
Bag._PHeight = isWin32 and 214 or 320 -- 容器可见区域 高
Bag._IW
根据你的需求,我来帮你修改代码,从获取装备改为获取指定物品,并将这些物品放入右侧容器中。
```lua
-- 修改:定义要检测的物品列表
Bag._targetItems = {
"九天玄气ツ阴阳逆乱",
"乾坤定星盘",
"鬼新娘の呼唤",
"帝乙木敕令",
"无极剑魂·道天一心"
}
-- 修改:从服务器获取物品数据
function Bag.GetItemData()
Bag._itemList = {}
for _, itemName in ipairs(Bag._targetItems) do
-- 通过物品名获取物品ID
local itemId = SL:GetMetaValue("ITEM_ID_BY_NAME", itemName)
if itemId then
-- 获取背包中该道具的数量
local itemCount = SL:GetMetaValue("ITEM_COUNT", itemId)
if itemCount and itemCount > 0 then
-- 获取背包中该道具的MakeIndex(唯一ID)
local makeIndex = SL:GetMetaValue("BAG_MAKEINDEX_BY_POS", itemId)
if makeIndex then
table.insert(Bag._itemList, {
id = itemId,
makeIndex = makeIndex,
name = itemName,
count = itemCount,
-- 获取物品图标
icon = SL:GetMetaValue("ITEM_ICON", itemId) or "res/public/1900000612.png"
})
end
end
end
end
SL:Print("获取到指定物品数量:" .. #Bag._itemList)
-- 更新显示
if Bag.UpdateItemDisplay then
Bag.UpdateItemDisplay()
end
end
-- 修改:更新物品显示函数
function Bag.UpdateItemDisplay()
if not Bag._itemSlots then
SL:Print("物品槽位未初始化")
return
end
for i = 1, 10 do
local slot = Bag._itemSlots[i]
if slot and slot.bg and not GUI:Win_IsNull(slot.bg) then
local itemData = Bag._itemList[i]
if itemData then
-- 有物品的情况
if slot.name and not GUI:Win_IsNull(slot.name) then
GUI:Text_setString(slot.name, itemData.name)
GUI:Text_setTextColor(slot.name, "#ffffff")
end
-- 设置物品图标
if slot.icon and not GUI:Win_IsNull(slot.icon) then
GUI:setVisible(slot.icon, true)
GUI:Image_loadTexture(slot.icon, itemData.icon)
end
-- 显示物品数量
if slot.count and not GUI:Win_IsNull(slot.count) then
GUI:Text_setString(slot.count, "x" .. itemData.count)
GUI:setVisible(slot.count, true)
end
else
-- 没有物品的情况
if slot.name and not GUI:Win_IsNull(slot.name) then
GUI:Text_setString(slot.name, "空")
GUI:Text_setTextColor(slot.name, "#808080")
end
if slot.icon and not GUI:Win_IsNull(slot.icon) then
GUI:setVisible(slot.icon, false)
end
if slot.count and not GUI:Win_IsNull(slot.count) then
GUI:setVisible(slot.count, false)
end
end
end
end
end
-- 修改:初始化物品容器函数
function Bag.InitItemContainer()
-- 获取物品滚动容器
Bag._itemScrollView = Bag._ui["ScrollView_equipment"]
if GUI:Win_IsNull(Bag._itemScrollView) then
SL:Print("物品滚动容器不存在")
return
end
-- 设置滚动方向为垂直
GUI:ScrollView_setDirection(Bag._itemScrollView, 1)
-- 设置容器内边距
GUI:ScrollView_setInnerContainerSize(Bag._itemScrollView, 100, 700)
-- 创建物品格子
Bag._itemSlots = {}
local slotWidth = 86
local slotHeight = 86
local startY = 700 - 100
local spacing = -20
for i = 1, 10 do
-- 创建格子背景
local slotBg = GUI:Image_Create(Bag._itemScrollView,
"item_slot_bg_" .. i,
10,
startY - (i-1) * (slotHeight + spacing),
"res/zhuansheng2.0/4.png")
-- 设置格子大小
GUI:setContentSize(slotBg, slotWidth, slotHeight)
-- 创建物品名称文本
local itemName = GUI:Text_Create(slotBg,
"item_name_" .. i,
slotWidth/2, -15,
12, "#ffffff", "空")
GUI:setAnchorPoint(itemName, 0.5, 0.5)
-- 创建物品图标
local itemIcon = GUI:Image_Create(slotBg,
"item_icon_" .. i,
slotWidth/2, slotHeight/2,
"")
GUI:setAnchorPoint(itemIcon, 0.5, 0.5)
-- 设置物品图标大小
GUI:setContentSize(itemIcon, slotWidth - 30, slotHeight - 30)
GUI:setVisible(itemIcon, false)
-- 创建物品数量文本
local itemCount = GUI:Text_Create(slotBg,
"item_count_" .. i,
slotWidth - 10, 10,
10, "#ffffff", "")
GUI:setAnchorPoint(itemCount, 1, 0.5)
GUI:setVisible(itemCount, false)
-- 添加点击事件
GUI:addOnClickEvent(slotBg, function()
Bag.OnItemSlotClick(i)
end)
-- 存储格子引用
Bag._itemSlots[i] = {
bg = slotBg,
icon = itemIcon,
name = itemName,
count = itemCount
}
end
-- 获取物品数据
Bag.GetItemData()
end
-- 修改:物品格子点击事件
function Bag.OnItemSlotClick(slotIndex)
SL:Print("点击物品格子:" .. slotIndex)
local itemData = Bag._itemList[slotIndex]
if itemData then
SL:ShowSystemTips("选中物品:" .. itemData.name .. " x" .. itemData.count)
-- 这里可以添加物品使用逻辑
-- SL:UseItem(itemData.id)
else
SL:ShowSystemTips("该槽位为空")
end
end
-- 在Bag.main函数中修改初始化部分
function Bag.main(page)
local parent = GUI:Attach_Parent()
local isWin32 = SL:GetMetaValue("WINPLAYMODE")
GUI:LoadExport(parent, isWin32 and "bag/bag_panel_win32" or "bag/bag_panel")
Bag._ui = GUI:ui_delegate(parent)
Bag._UI_ScrollView = Bag._ui["ScrollView_items"]
-- 初始化数据
Bag.Init(isWin32)
-- 修改:初始化物品容器(替换原来的装备容器)
Bag.InitItemContainer()
-- 适配
GUI:setPositionY(Bag._ui["Panel_1"], isWin32 and SL:GetMetaValue("PC_POS_Y") or SL:GetMetaValue("SCREEN_HEIGHT") / 2)
-- 界面拖动
GUI:Win_SetDrag(parent, Bag._ui["Image_bg"])
-- 界面浮起
GUI:Win_SetZPanel(parent, Bag._ui["Image_bg"])
GUI:addOnClickEvent(Bag._ui["Button_close"], function()
SL:CloseBagUI()
end)
-- 存入英雄背包
local Button_store_hero_bag = Bag._ui["Button_store_hero_bag"]
GUI:addOnClickEvent(Button_store_hero_bag, function()
local changeStoreMode = not Bag._changeStoreMode
if changeStoreMode then
local isActiveHero = SL:GetMetaValue("HERO_IS_ACTIVE")
if not isActiveHero then
return SL:ShowSystemTips("英雄还未激活")
end
local isCallHero = SL:GetMetaValue("HERO_IS_ALIVE")
if not isCallHero then
return SL:ShowSystemTips("英雄还未召唤")
end
end
Bag._changeStoreMode = changeStoreMode
GUI:Button_setGrey(Button_store_hero_bag, changeStoreMode)
end)
GUI:setVisible(Button_store_hero_bag, SL:GetMetaValue("USEHERO"))
-- 初始化左侧背包页签
Bag.InitPage()
Bag.PageTo(page or 1)
Bag.OnUpdateGold()
Bag.RegisterEvent()
-- 注册物品数据更新事件
SL:RegisterLUAEvent("ITEM_DATA_UPDATE", "Bag", function(data)
Bag.GetItemData()
end)
end
-- 修改注册事件部分
function Bag.RegisterEvent()
SL:RegisterLUAEvent(LUA_EVENT_MONEYCHANGE, "Bag", Bag.OnUpdateGold)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "Bag", Bag.OnClose)
--注册物品更新事件
SL:RegisterLUAEvent("ITEM_UPDATE", "Bag", function(data)
Bag.UpdateItemDisplay()
end)
end
function Bag.UnRegisterEvent()
SL:UnRegisterLUAEvent(LUA_EVENT_MONEYCHANGE, "Bag")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "Bag")
--取消注册物品更新事件
SL:UnRegisterLUAEvent("ITEM_UPDATE", "Bag")
end
```
主要修改内容:
1. **定义目标物品列表**:创建了`Bag._targetItems`数组,包含你要检测的5个物品名称
2. **修改数据获取函数**:`Bag.GetItemData()`通过物品名获取物品ID,然后检查背包中是否有这些物品
3. **更新显示逻辑**:`Bag.UpdateItemDisplay()`显示物品名称、图标和数量
4. **初始化容器**:`Bag.InitItemContainer()`创建10个物品格子,每个格子显示物品信息
5. **事件处理**:点击物品格子会显示物品详情
代码会检测背包中是否有"九天玄气ツ阴阳逆乱"、"乾坤定星盘"、"鬼新娘の呼唤"、"帝乙木敕令"、"无极剑魂·道天一心"这5个物品,如果有的话就会显示在右侧容器中,包括物品图标、名称和数量。