function EquipTipViewModel:GetCustomAttrStr()
local exAbil = self._itemData and self._itemData.ExAbil
if not exAbil or exAbil == "" or not next(exAbil) then
return ""
end
local customStr = ""
for p, d in pairs(exAbil.abil
```lua
function EquipTipViewModel:GetCustomAttrStr()
-- 获取装备的自定义属性数据(ExAbil)
local exAbil = self._itemData and self._itemData.ExAbil
-- 如果自定义属性不存在或为空,则返回空字符串
if not exAbil or exAbil == "" or not next(exAbil) then
return ""
end
-- 初始化自定义属性显示字符串
local customStr = ""
-- 遍历自定义属性组(abil 字段)
for p, d in pairs(exAbil.abil or {}) do
-- 过滤掉标题为“合成石”的属性组(特殊处理,不在此处显示)
if d.t and not string.find(d.t, "合成石") then
-- 判断是否显示标题
local isShowTitle = true
if (not d.t or d.t == "") then
isShowTitle = false
end
-- 如果显示标题,则格式化标题并添加颜色
if isShowTitle then
local title = d.t
local color = d.c
local colorHex = color and color > 0 and SL:GetValue("COLOR_BY_ID", color)
customStr = string.format("%s%s%s", customStr, customStr ~= "" and "\n" or "", colorHex and string.format("[color=%s]%s[/color]", colorHex, title) or title)
end
-- 获取属性组编号(i 字段)
local group = d.i or 0
-- 初始化属性列表,按位置(pos)分组
local attList = {}
-- 遍历属性值数组(v 字段)
for _, v in ipairs(d.v or {}) do
local color = v[1] or 0 -- 颜色ID
local attId = v[2] or 0 -- 属性ID(绑定表)
local value = v[3] or 0 -- 属性值
local percent = v[4] or 0 -- 是否为百分比(1 表示百分比)
local customId = v[5] -- 自定义描述ID
local pos = v[6] or 0 -- 自定义显示位置
-- 如果属性值大于0,则添加到对应位置的分组中
if value and value > 0 then
attList[pos] = attList[pos] or {}
table.insert(
attList[pos],
{color = color, attId = attId, value = value, percent = percent, pos = pos, customId = customId}
)
end
end
-- 将属性列表按位置(pos)排序
attList = SL:HashToSortArray(attList, function(a, b)
return a[1].pos < b[1].pos
end)
-- 遍历排序后的属性列表,生成属性显示字符串
for k, v in ipairs(attList or {}) do
-- 获取自定义描述(如果存在)
local customId = v[1] and v[1].customId or 0
local customDesc = SL:GetMetaValue("ITEMTIPS_CUSTOM_DESC", customId)
local color = 0
local attr = ""
-- 遍历同一位置下的多个属性(例如复合属性)
for i, a in ipairs(v) do
-- 获取属性配置
local attConfig = SL:GetMetaValue("ATTR_CONFIG", a.attId)
if not attConfig then
attConfig = {}
end
local value = a.value
local color = a.color
local percent = a.percent
-- 获取颜色值(16进制)
local colorHex = color > 0 and SL:GetValue("COLOR_BY_ID", color)
-- 如果属性类型为万分比(Type == 1),则转换值并标记为百分比
if attConfig.Type == 1 then
value = string.format("%.1f", value / 100) * 10 / 10
percent = 1
end
-- 如果存在自定义描述,则替换占位符并应用颜色
if customDesc then
local desc = value .. (percent > 0 and "%%" or "")
customDesc = string.gsub(customDesc, "%%s", desc, 1)
if colorHex then
-- 如果自定义描述中已有颜色标签,则替换;否则添加颜色标签
if string.find(customDesc, "%[color=(.-)%]") then
customDesc = string.gsub(customDesc, "%[color=(.-)%]", string.format("[color=%s]", colorHex))
else
customDesc = string.format("[color=%s]%s[/color]", colorHex, customDesc)
end
end
else
-- 没有自定义描述时,使用属性名称和值生成标准格式
local name = attConfig.Name
local attColor = attConfig.Color and SL:GetValue("COLOR_BY_ID", attConfig.Color)
local showColor = colorHex or attColor or "#FFFFFF"
local lineStr = string.len(attr) > 0 and "\n" or ""
attr = string.format("%s%s[color=%s]%s:+%s%s[/color]", attr, lineStr, showColor, name, value, percent > 0 and "%" or "")
end
end
-- 优先使用自定义描述,否则使用标准属性字符串
attr = customDesc or attr
-- 如果属性字符串不为空,则添加到总字符串中
if attr and attr ~= "" then
customStr = string.format("%s%s%s", customStr, customStr ~= "" and "\n" or "", attr)
end
end
end
end
-- 返回最终的自定义属性显示字符串
return customStr
end
```