--这一次遍历是为了求子节点和父节点的并集给到父节点 for i, v inpairs(gtRedDotConfig) do if v.parentT then local node = gtRedDotConfig[v.parentT] ifnot node.childNode then node.childNode = {} end node.childNode[i] = v end v.nMyId = i end
---BindMethodRD 绑定一个红点信息 ---@param nMethodId number gtRedDotId ---@param uObj UnityEngine.GameObject 红点GameObject functionBindMethodRD(nMethodId, uObj) local node = tRedDot[nMethodId] ifnot node then return end local bShow = node.cond() --如果自己没红点,查询子节点是否有红点 ifnot bShow and node.childNode then bShow = TraverseNode(node.childNode, true) end node.uRDObj = uObj --实现你的红点显隐方式 uObj:SetActive(bShow) end
---TraverseNode 遍历子节点,是否有红点显示 ---@param childNode table 子节点列表 functionTraverseNode(childNode) for _, v inpairs(childNode) do if v.cond() then returntrue elseif v.childNode then if TraverseNode(v.childNode) then returntrue end end end returnfalse end
这就是遍历子节点获取子节点的状态,只要有红点显示,直接更新到父节点。
1 2 3 4 5 6 7 8 9 10 11 12
---RelieveMethodRD 解除关联的红点信息 ---@param nRDId number gtRedDotId functionRelieveMethodRD(nRDId) ifnot nRDId then return end local t = gtRedDotConfig[nRDId] ifnot t then return end t.uRDObj = nil end
---UpdateMethodRD 更新一个红点信息 ---@param nRDId number gtRedDotId functionUpdateMethodRD(nRDId) local tPlayConfig = gtRedDotConfig[nRDId] ifnot tPlayConfig then return end local bShow = false local bInvoke = false if tPlayConfig.parentT then bShow = tPlayConfig.cond() ifnot bShow and tPlayConfig.childNode then bShow = TraverseNode(tPlayConfig.childNode, false) end bInvoke = true --如果是true,直接更新到root节点 if bShow then local node = tPlayConfig while node.parentT do node = gtRedDotConfig[node.parentT] node.uRDObj:SetActive(bShow) end else UpdateMethodRD(tPlayConfig.parentT) end end --更新自己 ifnot tPlayConfig.uRDObj then return end ifnot bInvoke then bShow = tPlayConfig.cond() end ifnot bShow and tPlayConfig.childNode then bShow = TraverseNode(tPlayConfig.childNode, false) end tPlayConfig.uRDObj:SetActive(bShow) end
--这里注册每个系统红点更新事件 for nId, tNode inpairs(gtPlayMethodConfig.tRedDot) do tNode.funcRDCallBack = function() UpdateMethodRD(nId) end for _, v inpairs(tNode.trigger) do --todo 实现你的红点触发事件,把他绑定到funcRDCallBack end end