local UIWindow = {} UIWindow.__index = UIWindow --> Services local ReplicatedStorage = game:GetService("ReplicatedStorage") --> Dependencies local UIList = require(ReplicatedStorage.Base.UIList) --> Variables local FolderWindows = ReplicatedStorage:WaitForChild("UI"):WaitForChild("Windows") local Utils = require(ReplicatedStorage.Tools.Utils) local LocalPlayer = game.Players.LocalPlayer local FolderPlayerGui = LocalPlayer:WaitForChild("PlayerGui") -------------------------------------------------------------------------------- function UIWindow:Init(UIManager: table, Data: table?) local self = {} self.UIManager = UIManager self.Data = Data self.Variables = {} self.Connections = {} self.SignalConnections = {} self.UIRootName = nil self.UIParentName = nil self.CloseDestroy = true -- 运行时操作变量 self.UIRoot = nil self.AutoInject = false return self end function UIWindow:SetData(Data: table?) self.Data = Data self:OnDataChanged() end -- 自动注入 function UIWindow:AutoInjectVariables() if not self.AutoInject then for varName, _ in pairs(self.Variables) do if typeof(varName) == "string" then local firstChar = string.sub(varName, 1, 1) local sixChar = string.sub(varName, 1, 6) local target if firstChar == "_" then if sixChar == "__list" then local prefab = Utils:FindInDescendantsUI(self.UIRoot, varName) target = UIList:Init(prefab) else -- _开头,递归查找 target = Utils:FindInDescendantsUI(self.UIRoot, varName) end if not target then error("自动注入失败:未找到UI节点 " .. varName) end self.Variables[varName] = target end end end self.AutoInject = true end end function UIWindow:OnOpenWindow() if not self.UIRoot then -- 创建UI根节点 self.UIRoot = FolderWindows:FindFirstChild(self.UIRootName):Clone() self.UIRoot.Parent = FolderPlayerGui:WaitForChild(self.UIParentName) print("节点", self.UIRoot, self.UIParentName, FolderPlayerGui, FolderPlayerGui:FindFirstChild(self.UIParentName)) else self.UIRoot.Visible = true self:Refresh() end self:AutoInjectVariables() end function UIWindow:OnCloseWindow() if self.CloseDestroy then self.UIRoot:Destroy() self.UIRoot = nil else self.UIRoot.Visible = false end end --> 覆盖用 function UIWindow:OnDataChanged() end function UIWindow:Refresh() end function UIWindow:Destroy() for _, connection in pairs(self.SignalConnections) do connection:DisconnectAll() end self.SignalConnections = nil for _, connection in pairs(self.Connections) do connection:Disconnect() end self.Connections = nil for k, v in pairs(self) do self[k] = nil end self = nil end return UIWindow