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") local FolderStarterPlayerScripts = game.StarterPlayer:WaitForChild("StarterPlayerScripts") local FolderUIInstanceScripts = FolderStarterPlayerScripts:WaitForChild("UI"):WaitForChild("InstanceScripts") -------------------------------------------------------------------------------- function UIWindow:Init(UIManager: table, Data: table?) local self = {} self.UIManager = UIManager self.Data = Data or {} 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 sevenChar = string.sub(varName, 1, 7) local target if firstChar == "_" then if sixChar == "__list" then local prefab = Utils:FindInDescendantsUI(self.UIRoot, varName) if not prefab then warn("没找到预制体", self.UIRoot, varName) end target = UIList:Init(prefab) target.TopUI = self elseif sevenChar == "__money" then target = Utils:FindInDescendantsUI(self.UIRoot, varName) -- 把脚本放到预制体下 local newScript = FolderUIInstanceScripts:FindFirstChild("Money"):Clone() newScript.Parent = target 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) else self.UIRoot.Visible = true self:Refresh() end self:AutoInjectVariables() end function UIWindow:OnCloseWindow() if self.SignalConnections then for _, connection in pairs(self.SignalConnections) do connection:DisconnectAll() end self.SignalConnections = nil end if self.Connections then for _, connection in pairs(self.Connections) do connection:Disconnect() end self.Connections = nil end if self.CloseDestroy then self.UIRoot:Destroy() self.UIRoot = nil else self.UIRoot.Visible = false end self:Destroy() end --> 覆盖用 function UIWindow:OnDataChanged() end function UIWindow:Refresh() end function UIWindow:Destroy() if self.TopUI then self.TopUI = nil end if self.SignalConnections then for _, connection in pairs(self.SignalConnections) do connection:DisconnectAll() end end self.SignalConnections = nil if self.Connections then for _, connection in pairs(self.Connections) do print("进入断开连接") connection:Disconnect() end end self.Connections = nil -- 销毁数据 if self.Data then if type(self.Data) == "table" then for k, v in pairs(self.Data) do self.Data[k] = nil end end self.Data = nil end -- 销毁实例内容 if self.Variables then for k, v in pairs(self.Variables) do if v.Destroy then v:Destroy() end v = nil end end for k, v in pairs(self) do self[k] = nil end self = nil end return UIWindow