local UIEntityBasePanel = UIViewClass() local EntityUIPositionModule = require("View/Battle/BattleEntityUI/EntityUIModule/EntityUIPositionModule") local EntityUIBaseModule = require("View/Battle/BattleEntityUI/EntityUIModule/EntityUIBaseModule") local EntityHeadUIModule = require("View/Battle/BattleEntityUI/EntityUIModule/EntityHeadUIModule") local DISTANCE_IN_TARGET = 35 * 35 function UIEntityBasePanel:OnCreated() self.elementDic = {} -- 用于保存Data数据方便在Updata中使用,这里的顺序不重要 self.elementPool = {} -- 用作简单池子 self.uiElementParams = nil -- 需要子类定义参数 end function UIEntityBasePanel:OnOpened(context) self:RegistSystemEvent(EventID.UIBattle_Element_Active, self.ElementActive) -- 激活名字板UI self:RegistSystemEvent(EventID.UIBattle_Element_ChangeName, self.ElementChangeName) -- 修改名字板UI名字 self:RegistSystemEvent(EventID.UIBattle_Element_ChangeColorName, self.ElementChangeColorName) -- 修改名字板UI名字 self:RegistSystemEvent(EventID.UIBattle_ExpEntity_UIDispose, self.ElementDispose) -- 移除名字板UI self:RegistSystemEvent(EventID.UIBattle_Element_UpdateHP, self.ElementUpdateHP) -- 修改名字板UI血条 self:RegistSystemEvent(EventID.UIBattle_Element_SetMaxDistSqrModelUI, self.SetMaxDistSqrModelUI) -- 设置显示最大距离 self:RegistSystemEvent(EventID.UI_HandleModelElement, self.SetHideType) -- 设置隐藏层级 self:RegistSystemEvent(EventID.UI_EnterCheatModel, self.EnterCheatModel) -- 进入cheat时隐藏头顶版 self:RegistSystemEvent(EventID.UI_ActiveAllModelElement, self.ActiveAllModelElement) -- 隐藏全部头顶版 end function UIEntityBasePanel:OnClosed() --将元素放回动态CollectionArray中,随界面自动销毁 for entity in pairs(self.elementDic) do self:ElementDispose(entity) end end function UIEntityBasePanel:OnLateUpdate(deltaTime, timeSinceUpdate) EntityUIPositionModule.OnLateUpdate(self, self.elementDic, self.uiElementParams) end function UIEntityBasePanel:CreateElementData(expEntity) local renderEntity = expEntity.RenderEntity local elementData = self.elementDic[renderEntity] if elementData then EntityUIBaseModule.ElementReset(expEntity, elementData) else local element = table.remove(self.elementPool) if not element then element = self:InstantiateCollection(self.ElementModelList, self.template_element.gameObject) end --首次加入一个元素时开启更新 if next(self.elementDic) == nil then self:StartRepeatFunc(self.OnLateUpdate, 0, -1, false) end elementData = EntityUIBaseModule.ElementCreate(expEntity, element) self.elementDic[renderEntity] = elementData end return elementData end function UIEntityBasePanel:CreatePetElementData(expEntity) local petRenderEntity = expEntity.PetRenderEntity if petRenderEntity == nil then return nil end local petElementData = self.elementDic[petRenderEntity] if petElementData then EntityUIBaseModule.ElementReset(expEntity, petElementData) else local element = table.remove(self.elementPool) if not element then element = self:InstantiateCollection(self.ElementModelList, self.template_element.gameObject) end --首次加入一个元素时开启更新 if next(self.elementDic) == nil then self:StartRepeatFunc(self.OnLateUpdate, 0, -1, false) end petElementData = EntityUIBaseModule.ElementCreate(expEntity, element) self.elementDic[petRenderEntity] = petElementData end return petElementData end -- 设置显示最大距离 function UIEntityBasePanel:SetMaxDistSqrModelUI(distance) if distance ~= nil then self.uiElementParams.MaxDistSqrModelUI = distance * distance else self.uiElementParams.MaxDistSqrModelUI = DISTANCE_IN_TARGET end end -- 设置隐藏层级(在子类中实现) function UIEntityBasePanel:SetHideType(hideLayer, hide) end -- 进入cheat时隐藏头顶版 function UIEntityBasePanel:EnterCheatModel(isEnter) UIUtils.SetActive(self.ElementModelList.gameObject, not isEnter) end -- 隐藏头顶版 function UIEntityBasePanel:ActiveAllModelElement(hide) UIUtils.SetActive(self.gameObject, hide) end ---------- Start 具体的子项Element操作 Start ---------- function UIEntityBasePanel:ElementActive(renderEntity, active) if renderEntity then EntityUIBaseModule.ElementActive(self.elementDic[renderEntity], active) end end function UIEntityBasePanel:ElementDispose(renderEntity) local elementData = self.elementDic[renderEntity] if elementData then --将头顶节点清除 self:ElementDestroyAllHeadUI(elementData) EntityUIBaseModule.ElementDispose(elementData) table.insert(self.elementPool, elementData.element) self.elementDic[renderEntity] = nil --列表为空时停止更新 if next(self.elementDic) == nil then self:StopRepeatFunc(self.OnLateUpdate) end end end function UIEntityBasePanel:SetUIOffsetY(renderEntity, offsetY) EntityUIBaseModule.SetUIOffsetY(self.elementDic[renderEntity], offsetY) end -- 改变名字(在子类中实现) function UIEntityBasePanel:ElementChangeName(renderEntity, name) end -- 改变名字字色,不变更宽度(在子类中实现) function UIEntityBasePanel:ElementChangeColorName(renderEntity, name) end -- 更新血条(在子类中实现) function UIEntityBasePanel:ElementUpdateHP(renderEntity, hp, pc, maxHp, init) end function UIEntityBasePanel:ElementTopShowTitle(renderEntity) local elementData = self.elementDic[renderEntity] if elementData then elementData.elementT:SetAsLastSibling() end end --刷新头顶UI,重新自适应位置 function UIEntityBasePanel:ElementRefreshHeadUI(renderEntity) local elementData = self.elementDic[renderEntity] if elementData and elementData.headUIDic then EntityHeadUIModule.AdjustHeadUI(elementData.headUIDic, self:GetModelNameTrans(renderEntity)) end end --头顶节点的创建,内部会对位置自适应校正 function UIEntityBasePanel:ElementCreateHeadUI(renderEntity, headUIType) local elementData = self.elementDic[renderEntity] if elementData then if elementData.headUIDic == nil then elementData.headUIDic = {} end local headUINode = elementData.headUIDic[headUIType] if headUINode then return headUINode end headUINode = EntityHeadUIModule.CreateHeadUI(headUIType, elementData.element.headUIGroup, self) elementData.headUIDic[headUIType] = headUINode EntityHeadUIModule.AdjustHeadUI(elementData.headUIDic, self:GetModelNameTrans(renderEntity)) return headUINode end return nil end --头顶节点设置参数 function UIEntityBasePanel:ElementSetHeadUI(renderEntity, headUIType, ...) local headUINode = self:ElementCreateHeadUI(renderEntity, headUIType) if headUINode then EntityHeadUIModule.SetHeadUIParam(headUIType, headUINode, ...) end end --移除头顶节点 function UIEntityBasePanel:ElementRemoveHeadUI(renderEntity, headUIType) local elementData = self.elementDic[renderEntity] if elementData then if elementData.headUIDic then local headUINode = elementData.headUIDic[headUIType] if headUINode then EntityHeadUIModule.DestroyHeadUI(headUINode,headUIType, self, elementData.element.headUIGroup) elementData.headUIDic[headUIType] = nil EntityHeadUIModule.AdjustHeadUI(elementData.headUIDic, self:GetModelNameTrans(renderEntity)) end end end end function UIEntityBasePanel:ElementDestroyAllHeadUI(elementData) EntityHeadUIModule.DestroyAllHeadUI(self, elementData.element.headUIGroup) elementData.headUIDic = nil end ------------ End 具体的子项Element操作 End ------------ return UIEntityBasePanel