BasePlayerGui

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建

BasePlayerGui 是所有 GUI 绘制存储类的抽象类。

属性

方法

GetGuiObjectsAtPosition

Instances

取得屏幕位置,并返回屏幕位置上所有的 GuiObject 实例,按屏幕顺序从上到下排列,作为第一个和最后一个索引,返回第一个和最后一个索引。

主要使用场景是在玩家的鼠标或触摸输入下获取 GUI 对象来执行“允许选择”或“高亮”等操作。 这些效果已经可以使用 GuiObject.MouseEnterGuiObject.MouseLeave 来实现,但这需要开发者在特定情况下追踪这些事件,即使他们只需要这个功能。

由于 BasePlayerGui 的子类对此函数继承,因此它可以被类对象,例如 PlayerGuiStarterGui 文件夹来发射。

参数

屏幕上的x位置与 GuiInset 应用后的左上角相对。

屏幕上的 y 位置与 GuiInset 应用后的左上角相对。


返回

Instances

一个包含 GuiObject 实例占用屏幕空间的表。

代码示例

All GUIs returned by BasePlayerGui:GetGuiObjectsAtPosition() are 'cloned' by the highlightAsFrame() local function, which creates a Frame GUI positioned on top of the specified GUI that is semi-transparent and the same GuiBase2d.AbsoluteSize and GuiBase2d.AbsolutePosition.

These highlights are added to the HighlightsContainer ScreenGui in the Highlight folder of the player's PlayerGui folder. Both are created by the code sample. All highlight GUIs are Instance:Destroy()ed every time highlightGui() executes.

Note that HighlightContainer's ScreenGui.DisplayOrder is 99999, a large number, so that it is unlikely any other GUI will render on top of the highlight GUIs.

Selecting GUIs at a Position

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
-- Create a Folder and ScreenGui to contain the highlight Frames
local highlights = Instance.new("Folder")
highlights.Name = "Highlights"
highlights.Parent = playerGui
local highlightsContainer = Instance.new("ScreenGui")
highlightsContainer.Name = "Container"
highlightsContainer.Parent = highlights
highlightsContainer.DisplayOrder = 99999
-- Creates a semi-transparent yellow Frame on top of the gui with the same AbsoluteSize and AbsolutePosition
local function highlightAsFrame(gui)
local highlight = Instance.new("Frame")
highlight.Name = "Highlight"
highlight.Parent = highlightsContainer
highlight.Size = UDim2.new(0, gui.AbsoluteSize.X, 0, gui.AbsoluteSize.Y)
highlight.Position = UDim2.new(0, gui.AbsolutePosition.X, 0, gui.AbsolutePosition.Y)
highlight.BackgroundColor3 = Color3.fromRGB(255, 255, 10) -- Yellow
highlight.BackgroundTransparency = 0.75
highlight.BorderSizePixel = 0
highlight.LayoutOrder = gui.LayoutOrder - 1
end
-- Use GetGuiObjectsAtPosition to get and highlight all GuiObjects at the input's position
local function highlightGui(input, _gameProcessed)
local pos = input.Position
local guisAtPosition = playerGui:GetGuiObjectsAtPosition(pos.X, pos.Y)
highlightsContainer:ClearAllChildren()
for _, gui in ipairs(guisAtPosition) do
if gui:IsA("GuiObject") then
highlightAsFrame(gui)
end
end
end
-- Fire highlightGui on InputBegan if input is of type MouseButton1 of Touch
local function InputBegan(input, gameProcessed)
local inputType = input.UserInputType
local touch = Enum.UserInputType.Touch
local mouse1 = Enum.UserInputType.MouseButton1
if inputType == touch or inputType == mouse1 then
highlightGui(input, gameProcessed)
end
end
UserInputService.InputBegan:Connect(InputBegan)

活动