紅點基本上是游戲中必備的吧,紅點遍地有,所以需要一套統一的紅點系統來管理,
lua中判斷Unity物件是否為空
--判斷unity物件是否為空, 如果你有些物件是在c#刪掉了,lua不知道
function IsNil(uobj)
if (uobj == nil) then
return true
end
if (uobj:Equals(nil)) then
return true
end
return false
end
紅點注冊
new_tip_register = {}
local this = new_tip_register
local LASTNUMBER = 99999
--紅點列舉(第一步:注冊紅點列舉)
Enum_New = {
NEW_ALL = 999999, --遍歷所有用
------------------------------------紅點-----------------------------------
NEW_CHANGESTATE_MAIN = 1, --主界面右下角切換按鈕
NEW_FACTION_MAIN = 2, --主界面幫會按鈕(主界面幫會入口ID)
NEW_FACTION_MEMBER = 3, --幫會成員
NEW_FACTION_MAIN_RED_PACKET = 4, --幫會成員紅包按鈕
NEW_FACTION_MEMBER_RECRUIT = 5, --幫會成員招募按鈕
NEW_FACTION_MEMBER_BUILDING = 6, --幫會建筑
NEW_FACTION_BUILD = 7, --幫會大廳
NEW_FACTION_BUILD_WEALFAREPANEL = 8, --幫會福利
NEW_FACTION_BUILD_SIGNE = 9, --幫會簽到
NEW_FACTION_BUILD_WEALFAREBOX = 10, --福利寶箱
NEW_FACTION_BUILD_ACCOUNT = 11, --幫會建筑賬房
NEW_FACTION_BUILD_ACCOUNT_WELFARE_ONE = 12, --幫會建筑福利
NEW_FACTION_BUILD_ACCOUNT_WELFARE_TWO = 13,
NEW_FACTION_MAIN_PANDECT = 14, --幫會總覽
NEW_FACTION_LEVEL_UP = 15, --總覽界面升級按鈕
--幫會大廳下的升級按鈕
NEW_FACTION_HALL_MAIN = 16, --幫會主廳(大廳)
NEW_FACTION_HALL_SHOP = 17, --幫會商店(大廳)
NEW_FACTION_HALL_BINGJIA = 18, --幫會兵甲(大廳)
NEW_FACTION_HALL_FANGJU = 19, --幫會防具(大廳)
NEW_FACTION_HALL_SHOUSHI = 20, --幫會首飾(大廳)
NEW_FACTION_HALL_SHOP_SELL = 21, --幫會商店物品上架
NEW_FACTION_BUILD_FACTIONSKILL = 22, --幫會技能
}
--[[--(第二步:注冊紅點列舉關系)
* @Description: 紅點關系串列(主要控制根節點紅點狀態,父節點狀態會根據子節點狀態重繪)
]]
local EnumNewList = {
--主界面右下角切換按鈕
[Enum_New.NEW_CHANGESTATE_MAIN] = {
--幫會
[Enum_New.NEW_FACTION_MAIN] = {
[Enum_New.NEW_FACTION_MAIN_PANDECT] = --總覽
{
[Enum_New.NEW_FACTION_LEVEL_UP] = LASTNUMBER, --升級
},
[Enum_New.NEW_FACTION_MEMBER] = { --成員
[Enum_New.NEW_FACTION_MEMBER_RECRUIT] = LASTNUMBER, --招募
[Enum_New.NEW_FACTION_MAIN_RED_PACKET] = LASTNUMBER, --紅包
},
[Enum_New.NEW_FACTION_MEMBER_BUILDING] = { --建筑
[Enum_New.NEW_FACTION_BUILD] = { --大廳
[Enum_New.NEW_FACTION_HALL_MAIN] = LASTNUMBER,
[Enum_New.NEW_FACTION_HALL_SHOP] = LASTNUMBER,
[Enum_New.NEW_FACTION_HALL_BINGJIA] = LASTNUMBER,
[Enum_New.NEW_FACTION_HALL_FANGJU] = LASTNUMBER,
[Enum_New.NEW_FACTION_HALL_SHOUSHI] = LASTNUMBER,
},
[Enum_New.NEW_FACTION_BUILD_ACCOUNT] = { --賬房
[Enum_New.NEW_FACTION_BUILD_ACCOUNT_WELFARE_ONE] = LASTNUMBER,
},
[Enum_New.NEW_FACTION_HALL_SHOP_SELL] = LASTNUMBER,
},
[Enum_New.NEW_FACTION_BUILD_WEALFAREPANEL] = { --福利
[Enum_New.NEW_FACTION_BUILD_SIGNE] = LASTNUMBER, --簽到
[Enum_New.NEW_FACTION_BUILD_FACTIONSKILL] = LASTNUMBER, --幫會技能
[Enum_New.NEW_FACTION_BUILD_WEALFAREBOX] = --寶箱
{
[Enum_New.NEW_FACTION_BUILD_ACCOUNT_WELFARE_TWO] = LASTNUMBER,
},
},
},
}
}
---------------外調-----------------
--獲得紅點列舉樹結構
function this.GetEnumNewList()
return EnumNewList
end
----------------------------------
紅點系統
--第一步:紅點列舉
--new_tip_register
--第二步:系結紅點顯示節點
--new_tip_register
require "logic/common/new_tip_register"
new_tip_system = {}
local this = new_tip_system
--變數宣告
local newInfoList = nil
local LASTNUMBER = 99999
--內部方法宣告
local InitNewTable = nil
local FreshAllRootNewState = nil
local SetActiveNew = nil
local RefreshCharacterAttr = nil
local SetOrGetStateEnum = nil
local PrintTrueLeaf = nil
local GetSubTree = nil
local curHeroLevel = nil
local EnumNewList = nil
---------------外調開始-----------
function new_tip_system.Init()
EnumNewList = {}
EnumNewList = new_tip_register.GetEnumNewList()
newInfoList = {}
InitNewTable(EnumNewList)
Notifier.regist(cmdName.MSG_REFRESH_CHARACTER_ATTR,RefreshCharacterAttr)
end
function new_tip_system.UnInit()
newInfoList = nil
EnumNewList = nil
Notifier.remove(cmdName.MSG_REFRESH_CHARACTER_ATTR,RefreshCharacterAttr)
end
--[[--(第三步:注冊重繪紅點狀態或gameobject)
* @Description: 注冊紅點狀態 注意:父節點只需要設定gameobject,如果設定了狀態也會被根節點狀態改變掉
@isActive 是否顯示(可為空)
@objNew 紅點GameObject(可為空)
@level 等級限制入口(比如入口等級,如果有等級限制Gameobject顯影的時候設定等級,沒有不設定)
]]
function new_tip_system.RegisterNewState(enumNew,isActive,objNew,level)
-- Trace("RegisterNewState:"..tostring(enumNew).." newInfoList:"..tostring(objNew).." isActive:"..tostring(isActive))
if curHeroLevel == nil then
curHeroLevel = data_center.GetPlayerUintValue(U_ACTOR_BASE_GRADE_ATTR)
end
if newInfoList ~= nil and newInfoList[enumNew] ~= nil then
local isNeedFresh = false
if isActive ~= nil then
if newInfoList[enumNew].state ~= isActive then
SetOrGetStateEnum(enumNew,isActive)
isNeedFresh = true
end
end
if not IsNil(objNew) then
newInfoList[enumNew].objNew = objNew
newInfoList[enumNew].widgetNew = componentGet(objNew.transform,typeof(UIWidget))
end
if level ~= nil then
newInfoList[enumNew].level = level
isNeedFresh = true
end
if isNeedFresh then
FreshAllRootNewState(EnumNewList,enumNew)
Notifier.dispatchCmd(cmdName.MSG_NEW_TIP_CHANGE,enumNew)
end
SetActiveNew(enumNew,SetOrGetStateEnum(enumNew))
end
end
function new_tip_system.RemoveRegisterNewState(enumNew)
if newInfoList[enumNew] ~= nil then
SetActiveNew(enumNew,false)
newInfoList[enumNew].objNew = nil
newInfoList[enumNew].widgetNew = nil
end
end
--[[--
* @Description: 獲取紅點狀態
]]
function new_tip_system.GetNewState(enumNew)
if not table.IsNullOrEmpty(newInfoList) and not table.IsNullOrEmpty(newInfoList[enumNew]) then
return newInfoList[enumNew].state
end
return false
end
--[[--
* @Description: 列印使所給節點為true的葉節點
]]
function new_tip_system.PrintTreeTrueLeaf(enumNew)
local subTree = GetSubTree(EnumNewList,enumNew)
if subTree == nil then
Trace(string.format("節點%s不存在",enumNew))
elseif SetOrGetStateEnum(enumNew) ~= true then
Trace(string.format("節點%s不為true",enumNew))
elseif subTree == LASTNUMBER then
Trace(string.format("節點%s為葉節點",enumNew))
elseif subTree ~= LASTNUMBER then
Trace("被查找的中間節點:"..tostring(enumNew))
PrintTrueLeaf(subTree)
end
end
---------------外調結束-----------
---------------內部函式開始-----------
function InitNewTable(newList)
for k,v in pairs(newList) do
local newItem = {}
newItem.objNew = nil
newItem.state = false
newInfoList[k] = newItem
if v ~= LASTNUMBER then
InitNewTable(v)
end
end
end
--[[--
* @Description: 重繪變更子節點所有父節點紅點的狀態
@newList:關系串列
@enumNew:變更紅點列舉
]]
function FreshAllRootNewState(newList,enumNew)
-- Trace("FreshAllRootNewState--------------:"..tostring(enumNew))
local result = false
local enumNewTarget = nil
for k,v in pairs(newList) do
local active = false
local target = nil
if v ~= LASTNUMBER then
if k ~= enumNew then
local tmpTarget = nil
active,tmpTarget = FreshAllRootNewState(v,enumNew)
active = SetOrGetStateEnum(k,active)
if tmpTarget ~= nil then
target = k
--重繪目標父節點狀態
SetActiveNew(k,active)
end
else
active = SetOrGetStateEnum(k)
target = k
end
else
active = SetOrGetStateEnum(k)
if k == enumNew then
target = k
end
end
if enumNew == Enum_New.NEW_ALL then
SetActiveNew(k,active)
end
--優化:減少搜索次數 找到變更節點并且result為true可以直接break(結果已知不用向下查找)
if active then
result = true
if enumNewTarget ~= nil then
break
end
end
if target ~= nil then
enumNewTarget = target
if result then
break
end
end
end
return result,enumNewTarget
end
function SetActiveNew(typeEnum,isActive)
if not IsNil(newInfoList[typeEnum].objNew) then
newInfoList[typeEnum].objNew:SetActive(isActive)
end
end
--[[--
* @Description: 設定或獲取紅點狀態(并根據等級回傳最終狀態,實際狀態還保存)
]]
function SetOrGetStateEnum(typeEnum,state)
if state ~= nil then
newInfoList[typeEnum].state = state
else
state = newInfoList[typeEnum].state
end
if newInfoList[typeEnum].level ~= nil and newInfoList[typeEnum].level > curHeroLevel then
state = false
end
return state
end
--[[--
* @Description: 玩家等級改變重繪紅點入口
]]
function RefreshCharacterAttr(param)
if param ~= nil then
for k,v in pairs(param) do
if k == U_ACTOR_BASE_GRADE_ATTR then
curHeroLevel = data_center.GetPlayerUintValue(U_ACTOR_BASE_GRADE_ATTR)
FreshAllRootNewState(EnumNewList,Enum_New.NEW_ALL)
break
end
end
end
end
--[[--
* @Description: 獲得對應節點的子樹
]]
function GetSubTree(newList,enumNew)
for k,v in pairs(newList) do
if k == enumNew then
return v
elseif v ~= LASTNUMBER then
local ret = GetSubTree(v,enumNew)
if ret ~= nil then
return ret
end
end
end
end
--[[--
* @Description: 列印狀態為true的葉節點
]]
function PrintTrueLeaf(newList)
for k,v in pairs(newList) do
if v ~= LASTNUMBER then
PrintTrueLeaf(v)
else
if SetOrGetStateEnum(k) == true then
Trace("為true的葉節點:"..tostring(k))
end
end
end
end
說明和使用
使用時需要注意Enum_New中的數值是唯一的,不要重復了,不然可能會導致子節點的紅點狀態變更了,但父節點的沒得到相應的重繪,
使用時呼叫new_tip_system.RegisterNewState(enumNew,isActive,objNew,level)即可,父節點的狀態會隨著子節點的狀態的改變而改變,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/300068.html
標籤:其他
上一篇:基于React + Antd 實作的斗獸棋web應用
下一篇:React官網入門專案井字棋游戲
