WidgetManager = WidgetManager or { AllPanel = {}, Initialized = false } function WidgetManager:Init() WidgetManager.Initialized = true self:InitBaseUI() for UIType,_ in pairs(WidgetConfig.Configs) do self:LoadUI(UIType) end end function WidgetManager:UnInit() for _, Panel in pairs(self.AllPanel) do if UE.IsValid(Panel) then Panel:RemoveFromParent() end end self.AllPanel = nil end function WidgetManager:ShowOrClosePanel(UIType, IsShow) if UGCGameSystem.IsServer() then return end if IsShow then self:ShowPanel(UIType) else self:ClosePanel(UIType) end end function WidgetManager:InitBaseUI() WidgetConfig.HideDefaultUI(true) end ---@param UIType int32 ---@return UUserWidget function WidgetManager:LoadUI(UIType) if not WidgetConfig.CheckUIType(UIType) then UGCLogSystem.LogError("[WidgetManager_LoadUI] UIType:%s Config is nil", tostring(UIType)) return nil end if self.AllPanel[UIType] ~= nil and UE.IsValid(self.AllPanel[UIType]) then return self.AllPanel[UIType] end local UIPath = WidgetConfig.GetWidgetPathFromUIType(UIType) local UIClass = UE.LoadClass(UIPath) local UILayer = WidgetConfig.GetWidgetLayer(UIType) if UIClass == nil then UGCLogSystem.LogError("[WidgetManager_LoadUI] UIClassName[%s] Load Failed!", UIPath) return nil end if UILayer == nil then UGCLogSystem.LogError("[WidgetManager_LoadUI] invalid UILayer, UIType=%d", UIType) return nil end local LocalPlayerController = UGCSystemLibrary.GetLocalPlayerController() if LocalPlayerController == nil or UE.IsValid(LocalPlayerController) == false then UGCLogSystem.LogError("[WidgetManager_LoadUI] invalid local PlayerController") return end local UIWidget = UserWidget.NewWidgetObjectBP(LocalPlayerController, UIClass) if UIWidget ~= nil then UIWidget:AddToViewport(UILayer) UIWidget:SetVisibility(ESlateVisibility.Collapsed) self.AllPanel[UIType] = UIWidget if table.hasValue(WidgetConfig.CustomWidget, UIType) then UGCWidgetManagerSystem.AddChildToTochButton(UIWidget); end return UIWidget end return nil end --- 显示 UI ---@param UIType int32 ---@param NeedClosePeerPanel bool 是否关闭其他相同类型的UI ---@param ... table function WidgetManager:ShowPanel(UIType, NeedClosePeerPanel, ...) if not WidgetConfig.CheckUIType(UIType) then UGCLogSystem.LogError("[WidgetManager_ShowPanel] UIType:%s Config is nil", tostring(UIType)) return nil end if NeedClosePeerPanel == nil then NeedClosePeerPanel = true end local Panel = self:GetPanel(UIType) if Panel ~= nil and UE.IsValid(Panel) then if NeedClosePeerPanel then self:ClosePeerPanel(UIType) end Panel:SetVisibility(ESlateVisibility.SelfHitTestInvisible) if Panel["OnShowPanel"] ~= nil then Panel:OnShowPanel(...) end else UGCLogSystem.LogError("[WidgetManager_ShowPanel] Show UI Failed: %s", tostring(UIType)) end end ---@param UIType int32 ---@param WorldPosition Vector3D @UI生成的三维世界坐标 function WidgetManager:ShowUIWithWorldPosition(UIType, WorldPosition) if WorldPosition == nil then UGCLogSystem.LogError("[WidgetManager] WorldPosition is nil") return end self:ShowPanel(UIType) end --- 关闭同层级的所有UI ---@param InUIType int32 function WidgetManager:ClosePeerPanel(InUIType) local CurLayer = WidgetConfig.GetWidgetLayer(InUIType) local AllLayerWidget = WidgetConfig.GetAllWidgetTypeFromLayer(CurLayer) for i, UIType in pairs(AllLayerWidget) do if UIType ~= InUIType then self:ClosePanel(UIType) end end end ---@param UIType int32 function WidgetManager:ClosePanel(UIType) UGCLogSystem.Log("[WidgetManager_ClosePanel] UIType:%s", tostring(UIType)) if not self:PanelIsValid(UIType) then return end local Panel = self.AllPanel[UIType] if Panel:GetVisibility() ~= ESlateVisibility.Collapsed then Panel:SetVisibility(ESlateVisibility.Collapsed) if Panel["OnClosePanel"] ~= nil then Panel:OnClosePanel() end if WidgetConfig.GetWidgetShowOnce(UIType) then self:DestroyPanel(UIType); end end end function WidgetManager:CloseAllPanel() for UIType, _ in pairs(self.AllPanel) do self:ClosePanel(UIType) end end function WidgetManager:DestroyPanel(UIType) if not self:PanelIsValid(UIType) then return end local Panel = self.AllPanel[UIType] Panel:RemoveFromParent() self.AllPanel[UIType] = nil end --- 销毁所有 UI function WidgetManager:DestroyAllPanel() for UIType, _ in pairs(self.AllPanel) do self:DestroyPanel(UIType) end end ---@return bool 是否存在UI _ function WidgetManager:PanelIsValid(UIType) local Panel = self.AllPanel[UIType] return Panel ~= nil and UE.IsValid(Panel) end --- 获取UI function WidgetManager:GetPanel(UIType) local Panel = self.AllPanel[UIType] if Panel ~= nil and UE.IsValid(Panel) then return Panel else return self:LoadUI(UIType) end end function WidgetManager:IsVisiblePanel(UIType) if self:PanelIsValid(UIType) then local Panel = self.AllPanel[UIType] return Panel:IsVisible() end return false end function WidgetManager:VisiblePanel(UIType) if self:PanelIsValid(UIType) then local Panel = self.AllPanel[UIType] Panel:SetVisibility(ESlateVisibility.SelfHitTestInvisible) end end function WidgetManager:HidePanel(UIType) if self:PanelIsValid(UIType) then local Panel = self.AllPanel[UIType] Panel:SetVisibility(ESlateVisibility.Collapsed) end end return WidgetManager